<?php 
namespace App\Http\Controllers\TongGaoApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\tonggao\article;
use App\Models\Mysql\tonggao\term;
use App\Models\Mysql\tonggao\term_detail;
use App\Models\Mysql\tonggao\sentence;
use Symfony\Component\DomCrawler\Crawler;

use Excel;
use App\Http\Controllers\TongGaoApi\ExcelActionController;

class td_temp{
	public $article_id, $seq_no, $old_key, $new_key;
}

class PrepareController extends Controller
{
//	use GlobalData;

	const ROUTE_PREFIX = '/api/*';

	protected $request;

	protected $curTime;

	protected $debug;


	/**
	 * 创建新的`DictController`控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		if(config('app.debug')) {
			$this->debug = true;
			error_reporting(E_ALL | E_STRICT);
		} else {
			$this->debug = false;
		}
	}

    public function importArticle()
    {
		set_time_limit(0);
		$batch=2;
		$debug1=false;$debug2=false;
		$dryRun = true;
		//$fileNameArr = array("01_01","01_02");//,"01_03","02_01","02_02","03_01","03_02");
		//$fileNameArr = array("01_01","01_02","01_03","02_01","02_02","03_01","03_02");
		 //array("01_01","01_02","01_03","01_04","02_01","02_02");//BOOK7
		$bookNo = 4;
		switch($bookNo){
			case 4:
				$fileNameArr = array("01_01","01_02","01_03","02_01","02_02","03_01","03_02");//BOOK6
				break;
			case 5:
				$fileNameArr = array("01_01","01_02","01_03","02_01","02_02","03_01","03_02","01_preface");//BOOK6
				break;
			case 6:
				$fileNameArr = array("01_01","01_02","01_03","02_01","02_02","03_01","03_02","04_01","04_02","05_01","05_02","02_preface","05_preface");//BOOK6
				break;
			case 7:
				$fileNameArr = array("01_01","01_02","01_03","01_04","02_01","02_02");//BOOK7
				break;
			case 8:
				$fileNameArr = array("01_01","01_02","02_01","02_02","03_01","03_02",);//BOOK8
				break;
		}
		$articleTableName = ($bookNo>=7 && $batch==1)?"article_78":"article";
		$sentenceTableName = ($bookNo>=7 && $batch==1)?"sentence_78_ref":"sentence";
		$importFromHTML = true;
		
		$articleCount = count($fileNameArr);
		if ($bookNo>=7){
			for($i=0;$i<$articleCount;$i++){
				$fileNameArr[$i+$articleCount] = $fileNameArr[$i]."_ref";
				//$fileNameArr[$i] = $fileNameArr[$i]."_ref";
			}
		}
		//var_dump($fileNameArr);exit;
		
		foreach($fileNameArr as $k=>$fileNamePart){
			/* only for 06_03_2
			if ($fileNamePart=="03_02"){
				continue;
			}*/
			$fileName = sprintf("F:\\000_booktran\\fromHuoYun\\book%1d\\%1d_%s",$bookNo,$bookNo,$fileNamePart);
			if($importFromHTML){
				$fileName .= ".html";
			} else{
				$fileName .= ".txt";
			}
			
			$temp_r= article::select("select id from $articleTableName where name=\"文集{$bookNo}_$fileNamePart\"");
			if (empty($temp_r)){
				echo "Error: name=\"文集{$bookNo}_$fileNamePart\" not found in $articleTableName table</br>";
				exit;
			}
			$article_id = $temp_r[0]->id;	//echo "article_id=$article_id</br>";
			
			//$sql = "delete from $sentenceTableName where article_id=".$article_id;
			//sentence::update($sql);
			$file=file_get_contents($fileName);
			//echo mb_strlen($file);
			if($importFromHTML){
				$crawler = new Crawler();
				$crawler->addHtmlContent($file,'UTF-8');
				$lines=array();
				$domCount=count($crawler->filterXPath('//font[@*]'))/3;
				for($i=1;$i<=$domCount;$i++){
					$sentDom=$crawler->filterXPath('//font[@id="sent-'.$i.'"]');//getElementById("sent-"+i);
					//echo "i=$i------------</br>";echo $sourceDom->text()."</br>";echo $targetDom->text()."</br>";
					$sourceDom=$sentDom->filterXPath('//font[@class="source"]');
					$targetDom=$sentDom->filterXPath('//font[@class="target"]');
					$lines []= $sourceDom->text()."\t".$targetDom->text();
				}
			} else{
				$lines = explode("\r\n",$file);
				echo count($lines)."</br>";
			}

			$totalChnCharCount = 0;
			foreach($lines as $lineno=>$line){
				$fields = explode("\t",$line);
				//echo $fields[0]."</br>";
				//echo $fields[1]."</br>";
				//echo $lineno;
				$totalChnCharCount += mb_strlen($fields[0]);
				//if ( !empty($fields[0]) && !empty($fields[1]) && strlen($fields[1])> 8){
					
				$temp_r = sentence::select(sprintf("select * from sentence where article_id=%d and seq_no=%d",$article_id,$lineno+1));
				if (empty($temp_r)){
					if($debug1)	echo "Not exist: {$fields[0]}/{$fields[1]}</br>";
					if (strlen($fields[1])> 8)
						echo "Warning strlen(english)>8 in $fileNamePart</br>";
					if (!$dryRun){
						$field[0] = (!empty($field[0])) ? $field[0] : "";
						$field[1] = (!empty($field[1])) ? $field[1] : "";
						$sql = "insert INTO $sentenceTableName (article_id, seq_no, content, translation, batch) VALUES (".
							implode(",",array($article_id,$lineno+1,
								'"'.addslashes($fields[0]).'"',
								'"'.addslashes($fields[1]).'"',
								$batch)).
							")";
						sentence::select($sql);
					}
				}
				else{
					if ((trim($temp_r[0]->content)==$fields[0]) &&(trim($temp_r[0]->translation)==$fields[1])){
						if ($debug2)	echo "Exist: all correct</br>";
					}
					else{
						if ($debug1) echo "Exist: different {$fields[0]}/{$temp_r[0]->content}/{$fields[1]}/{$temp_r[0]->translation}</br>";
					}
					
					if (count($temp_r)<>1)
						echo "Count error</br>";
				}	
			}
			echo "$fileNamePart Total Chinese Character Count=$totalChnCharCount</br>";
		}
		
	}
	
    public function importTerm()
    {
		set_time_limit(0);
		$fileNameArr = array("WangLiZhong_0221");//array("DingFuBao","term_wj_5","LiuXueLing_0223");
		
		foreach($fileNameArr as $k=>$fileNamePart){
			switch($fileNamePart){
				case "DingFuBao":
					$source = "DFB";
					break;
				case "term_wj_5":
					$source = "wj_5";
					break;
				case "LiuXueLing_0223":
					$source = "liuxl_0223";
					break;
				case "WangLiZhong_0221":
					$source = "wanglz_0221";
					break;
					
			}

			$fileName = "F:\\000_booktran\\TermLibRaw\\".$fileNamePart.".txt";
			$sql = "delete from term_raw where source=".'"'.$source.'"';
			term::select($sql);
			$file=file_get_contents($fileName);
			//echo mb_strlen($file);
			//折行处理 - NOT DONE
			//开始折行 判断最后一个field的第一个字符，转换到lineContinue
			//折行中，保持$lineContinue
			//折行结束，转换到$lineContinue==false
			//普通，保持$lineContinue==false

			$lines = explode("\r\n",$file);
			echo count($lines)."</br>";
			foreach($lines as $lineno=>$line){
				$fields = explode("\t",$line);
				//echo $fields[0]."</br>";
				//echo $fields[1]."</br>";
				//echo $lineno;
				$fields_sql=array();
				foreach($fields as $field){
					$field = str_replace("\r"," ",$field);
					$field = str_replace("\n"," ",$field);
					if (mb_substr($field,0,1)=='"' && mb_substr($field,mb_strlen($field)-1,1)=='"')
						$fields_sql []= '"'.addslashes(mb_substr($field,1,mb_strlen($field)-2)).'"';
					else
						$fields_sql []= '"'.addslashes($field).'"';
				}
				$sql="";
				switch($source){
					case "DFB":
						$sql = "insert INTO term_raw (`source`,`chinese_key`, `chinese_explanation`) VALUES (".
									implode(",",array(
										'"'.$source.'"',
										'"'.addslashes($fields[0]).'"',
										'"'.addslashes($fields[1]).'"'
										)
									).
								")";
					case "wj_5":
						if (count($fields)==2){
							$sql = "insert INTO term_raw (`source`,`chinese_key` ) VALUES (".
										implode(",",array(
											'"'.$source.'"',
											'"'.addslashes($fields[0]).'"'
											)
										).
									")";
						}
						break;

					case "liuxl_0223":
					case "wanglz_0221":
							$sql = "insert INTO term_raw (`source`,`chinese_key` ) VALUES (".
										implode(",",array(
											'"'.$source.'"',
											'"'.addslashes($fields[0]).'"'
											)
										).
									")";
						break;
				}
				if (!empty($sql))
					term::select($sql);
			}
			
			//delete duplicate
			$sql='select id, chinese_key from term_raw where source="'.$source.'" order by chinese_key';
			$r = term::select($sql);
			$prevTerm="NOTEXIST";
			foreach($r as $term){
				if ($term->chinese_key==$prevTerm){
					$sql = "delete from term_raw where id=".$term->id;
					term::select($sql);
					echo "$prevTerm is duplicate so delete it </br>";
					
				} else{
					$prevTerm=$term->chinese_key;
				}
			}
		}
	}
	
	public function addTerm()
    {
		set_time_limit(0);
		$option = "jls_78";
		switch($option){
			case "wanglz_0221":
				$sql = 'select chinese_key,source from term_raw where source = "wanglz_0221"';
				break;
				
			case "jls_78":
				$sql = 'select * from jlsterm_78';
				break;
		}
		$rawList = term::select($sql);
		foreach($rawList as $rawItem){
			switch($option){
				case "wanglz_0221":
					$rawSource = $rawItem->source;;
					break;
					
				case "jls_78":
					$rawSource=$option;
					break;
			}

			$sql = 'select * from term where chinese_key="'.$rawItem->chinese_key.'"';
			$termInfo = term::select($sql);
			
			if (empty($termInfo)){
				echo "NOT EXIST {$rawItem->chinese_key}</br>";
				$sql = "insert INTO term(
							chinese_key, source) 
							VALUES (".
								implode(",",array(
									'"'.$rawItem->chinese_key.'"',
									'"'.$rawSource.'"',
								)).
							")";
				term::select($sql);
			} else{
				$termInfo=$termInfo[0];
				$curSourceArr = explode(",",$termInfo->source);
				
				if (!in_array($rawSource, $curSourceArr)){
					$newSource = empty($termInfo->source)? $rawSource : ($termInfo->source.",".$rawSource);
					echo "EXIST & ADD SOURCE {$rawItem->chinese_key}</br>";
					$sql = 'update term SET source = "'.$newSource.'" where id='.$termInfo->id;
					term::select($sql);
				}
				else
					echo "EXIST & ALREADY IN SOURCE {$rawItem->chinese_key}</br>";
			}
		}
	}

	public function addTerm_v1()
    {
		set_time_limit(0);
		$sql = 'delete from term where id>=8';
		term::select($sql);

		$sql = 'select chinese_key,source from term_raw where chinese_key not like "%[%]%"';
		$rawList = term::select($sql);
		foreach($rawList as $rawItem){
			$sql = 'select id,source from term where chinese_key="'.$rawItem->chinese_key.'"';
			$termInfo = term::select($sql);
			if (empty($termInfo)){
				$sql = "insert INTO term(
							chinese_key, source) 
							VALUES (".
								implode(",",array(
									'"'.$rawItem->chinese_key.'"',
									'"'.$rawItem->source.'"',
								)).
							")";
				term::select($sql);
			} else{
				$termInfo=$termInfo[0];
				$curSourceArr = explode(",",$termInfo->source);
				if (!in_array($rawItem->source, $curSourceArr)){
					$newSource = empty($termInfo->source)? $rawItem->source : ($termInfo->source.",".$rawItem->source);
					$sql = 'update term SET source = "'.$newSource.'" where id='.$termInfo->id;
					term::select($sql);
				}
			}
		}
	}
    public function fillTermDetail()
    {
		set_time_limit(0);
		//incremental mode
		$dryRun = false;
		$debug1 = true;
		$debug2 = true;
		
		//$option = "batch30"; $termBatch=3; $termDetailBatch=30;
		//$option = "batch32"; $termBatch=3; $termDetailBatch=30;
		$option = "batch900";  $termDetailBatch=900;
		$sqlTermLength = "length(chinese_key)>3";
		$sqlTermBatch = "term.batch>0";
		switch($option){
			case "batch30":
				$termTableName = "term";
				$termDetailTableName = "term_detail";
				$sqlTerm = " $sqlTermLength AND $sqlTermBatch order by chinese_key_len desc";
				$sqlSentence = "1";// all the books
				break;
				
			case "batch31":
				$termTableName = "term";
				$termDetailTableName = "term_detail";
				$sqlTerm = " $sqlTermLength AND $sqlTermBatch AND chinese_key in (\"学修\")";
				$sqlSentence = "1";// all the books
				break;
 				
			case "batch32":
				$termTableName = "term";
				$termDetailTableName = "term_detail";
				$sqlTerm = " $sqlTermLength AND $sqlTermBatch AND chinese_key in (\"无情\")";
				$sqlSentence = "1";// all the books
				break;

			case "batch900":
				$termTableName = "term";
				$termDetailTableName = "term_detail";
				$sqlTerm = " $sqlTermBatch order by length(chinese_key) desc";
				$sqlSentence = "article_id=56";// gl
				break;
				
		}

		$termSql = "select id, chinese_key, batch, source from $termTableName where $sqlTerm";
		$termList = term::select($termSql);
		
		foreach($termList as $term){
			if ($debug1)	echo "id=".$term->id."chinese_key=".$term->chinese_key."</br>";
			
			$needle = $term->chinese_key;
			$sentenceSql = "select id, article_id, content, batch from sentence where content like \"%$needle%\" and $sqlSentence";
			$sentenceList = sentence::select($sentenceSql);
			//if ($debug1)	echo "$sentenceSql</br>";
			foreach($sentenceList as $sentence){
				$hay = $sentence -> content;
				$sentence_id = $sentence -> id;
				if ($debug2)	echo "sentence=$hay</br>";
				$curPos=0;
				$cur_occur_no=$cur_occur_no_big=0;
				$hayLen=mb_strlen($hay);
				do{
					$curFoundPos = mb_strpos($hay,$needle,$curPos);
					if ($curFoundPos===FALSE){
						break;
					}
					
					//construct $bigSqls
					$bigSqls = array();
					$bigSqlChineseKeyClause = "chinese_key like \"%$needle%\" and term.id<>{$term->id}";
					switch($option){
						case "batch30":
						case "batch31":
						case "batch900":
							$bigSqls []= "select term_id big_term_id, term_detail.id big_term_detail_id, chinese_key from $termTableName, $termDetailTableName where term.id=term_detail.term_id and sentence_id=$sentence_id and $sqlTermBatch and $bigSqlChineseKeyClause";
							break;
					}
					
					$foundInBig = false;
					$big_term_infos = array();
					foreach($bigSqls as $bigSql){
						//if ($debug2)	echo "$bigSql</br>";
						$tempBigs = term::select($bigSql);
						if (!empty($tempBigs))
							if ($debug1)	echo count($tempBigs)."</br>";
						foreach($tempBigs as $tempBig){
							$bigNeedle = $tempBig->chinese_key;
							//echo "$bigNeedle</br>";
							$smallInBigPos = mb_strpos($bigNeedle,$needle);
							$bigTermPosInSentence = $curFoundPos - $smallInBigPos;
							//echo "$bigTermPosInSentence</br>";
							if ($bigTermPosInSentence<0)
								continue;
							$bigPieceInSentence = mb_substr($hay,$bigTermPosInSentence,mb_strlen($bigNeedle));
							//echo "$bigPieceInSentence</br>";
							if ($bigPieceInSentence==$bigNeedle){
								if ($debug2)	echo "Big found! $needle contained in $bigNeedle</br>";
								$foundInBig=true;
								$big_term_infos []= (array('big_term_id'=>$tempBig->big_term_id,'big_term_detail_id'=>$tempBig->big_term_detail_id));
							}
						}
						flush();ob_flush();
					}

					$has_big_term = (empty($big_term_infos)) ? 0 : 1;
					$termDetailSql = "select id, chinese_pos from $termDetailTableName where term_id={$term->id} and sentence_id={$sentence->id} and chinese_pos=$curFoundPos";
					if ($debug1)	echo "$termDetailSql</br>";
					$temp_r = term_detail::select($termDetailSql);
					if (!empty($temp_r)){
						//echo "existing</br>";
						$term_detail = $temp_r[0];
						$cur_term_detail_id = $term_detail->id;
						$insertUpdateSql = "update $termDetailTableName SET occur_no=$cur_occur_no, occur_no_big=$cur_occur_no_big, has_big_term=$has_big_term  where id=$cur_term_detail_id";
					}
					else{
						//echo "not existed</br>";
						$insertUpdateSql = "insert INTO $termDetailTableName (chinese_pos, occur_no, occur_no_big, has_big_term,batch, sentence_id, article_id, term_id) VALUES (".
						implode(",",array($curFoundPos,$cur_occur_no,$cur_occur_no_big, $has_big_term, $termDetailBatch, $sentence->id,$sentence->article_id,$term->id)).")";
						
						if (($sentence->batch==1) && ($term->batch<3) && ($term->source<>"jls"))	echo "HIHIHIHI $hay</br>";
					}
					
					if($dryRun){
						if ($debug1)	echo "$hay</br>";
						if ($debug1)	echo "$insertUpdateSql</br>";
					}else{
						if (!empty($insertUpdateSql)){
							if ($debug1)	echo "$insertUpdateSql</br>";
							term_detail::select($insertUpdateSql);
							if ($has_big_term){
								if (empty($temp_r)){
									$temp_r = term_detail::select($termDetailSql);
									if (empty($temp_r)){
										echo "Error:NOT FOUND after insert</br>";
										continue;
									}
									$term_detail = $temp_r[0];
									$cur_term_detail_id = $term_detail->id;
								}
								
								foreach($big_term_infos as $big_term_info){
									$cur_big_term_id = $big_term_info['big_term_id'];
									$cur_big_term_detail_id = $big_term_info['big_term_detail_id'];
									
									$temp_r = term::select("select term_detail_big_term_id from term_detail_big_term where term_detail_id=$cur_term_detail_id and big_term_id=$cur_big_term_id AND big_term_detail_id=$cur_big_term_detail_id");
									if (empty($temp_r)){
										term::select("INSERT into term_detail_big_term (term_detail_id,big_term_id, big_term_detail_id) VALUES ($cur_term_detail_id,$cur_big_term_id,$cur_big_term_detail_id)");
									}
								}//end of loop big_term_info	
							}
							if ($debug1)	echo "inserted/updated</br>";
						}
					}
						
					$cur_occur_no++;
					if ($has_big_term)
						$cur_occur_no_big++;

					$curPos=$curFoundPos+1;
				}while($curPos<$hayLen);
			}//end of loop sentence
		}//end of loop term
    }//end of function FillTermDetail
	
/*old version 2, workable	
    public function fillTermDetail()
    {
		set_time_limit(0);
		$fillMode = 1; //0:re-build, 1:incremental
		$dryRun = true;
		$debug1 = true;
		$debug2 = true;
		
		//$option = "极乐寺法师提供的7-8册术语，在4-6册出现，但是没有标记为术语";
		$option = "mtg_20180329_part2";
		switch($option){
			case "option_1":
				$termTableName = "term";
				$termDetailTableName = "term_detail";
				$sqlTerm = "chinese_key in (\"修行经验\")";//同行善友\"佛教徒\",\"修行人\",\"菩萨摩诃萨\",\"修行者\",
				$sqlSentence = "article_id between 4 and 28";// and sentence.id=540
				break;
				
			case "极乐寺法师提供的7-8册术语，在4-6册出现，但是没有标记为术语":
				$fillMode = 0; //0:re-build, 1:incremental
				$termTableName = "jlsterm_78";
				$termDetailTableName = "jls456_term_detail";
				$sqlTerm = " in456=1 AND in456term=0 order by length(chinese_key) desc";//and chinese_key in (\"行\",\"行谊\") 
				$outputFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\book7&8_term\\七八册术语整合_notin456term.txt");		
				term::select("delete from $termDetailTableName");
				file_put_contents($outputFileName,"");
				$sqlSentence = "article_id between 4 and 28";// and sentence.id=540
				$checkFoundInBig = true;
				$bigSql1 = true;
				break;
			
			case "mtg_20180329_part1":
				$fillMode = 1; //0:re-build, 1:incremental
				$termTableName = "term";
				$termDetailTableName = "term_detail";
				$sqlTermPhrase = 	'"戒律","律仪","波罗提木叉","别解脱律仪","别解脱戒","威仪","律藏","开遮","持戒","犯戒","制戒","结戒","戒本","性戒","遮戒","戒法","戒体","戒行","戒相","八关斋戒","沙弥戒","沙弥尼戒","比丘戒","比丘尼戒","菩萨戒","三聚戒","三聚净戒","摄律仪戒","摄善法戒","摄众生戒","饶益有情戒"';
				//$sqlTermPhrase = "\"戒律\",\"律仪\",\"波罗提木叉\",\"别解脱律仪\",\"别解脱戒\",\"威仪\",\"律藏\",\"开遮\",\"持戒\",\"犯戒\",\"制戒\",\"结戒\",\"戒本\",\"性戒\",\"遮戒\",\"戒法\",\"戒体\",\"戒行\",\"戒相\",\"八关斋戒\",\"沙弥戒\",\"沙弥尼戒\",\"比丘戒\",\"比丘尼戒\",\"菩萨戒\",\"三聚戒\",\"三聚净戒\",\"摄律仪戒\",\"摄善法戒\",\"摄众生戒\",\"饶益有情戒\"";
				$sqlTerm = "chinese_key in ($sqlTermPhrase) order by chinese_key_len desc";
				$sqlSentence = "article_id between 29 and 52";// book7-8
				$checkFoundInBig = true;
				$bigSql1 = true;
				$bigSql2 = true;
				break;

			case "mtg_20180329_part2":
				$fillMode = 1; //0:re-build, 1:incremental
				$termTableName = "term";
				$termDetailTableName = "term_detail";
				$sqlTermPhrase = '"戒条","根律仪","别解律仪","尸罗律仪","律典","戒律仪"';
				$sqlTerm = "chinese_key in ($sqlTermPhrase) order by chinese_key_len desc";
				$sqlSentence = "article_id between 4 and 52";// book4-8
				$checkFoundInBig = true;
				$bigSql1 = true;
				$bigSql2 = true;
				break;
		}
		//$sqlTerm = "(phrase=1 or category=\"buddhaname\" or category=\"patriarchname\")";

		$sql = "select id, chinese_key from $termTableName where $sqlTerm";
		$termList = term::select($sql);
		
		foreach($termList as $term){
			if ($debug1)	echo "id=".$term->id."chinese_key=".$term->chinese_key."</br>";
			if (($option<>"极乐寺法师提供的7-8册术语，在4-6册出现，但是没有标记为术语") &&($fillMode==0)){
				if(!empty($sqlSentence)){
					$sql = "delete from $termDetailTableName where term_id={$term->id} and $sqlSentence";
					if (!$dryRun){
						term_detail::select($sql);
					}
				}
			}
			
			$needle = $term->chinese_key;
			$sql = "select id, article_id, content from sentence where content like \"%$needle%\" and $sqlSentence";
			$sentenceList = sentence::select($sql);
			if ($debug1)	echo "$sql</br>";
			foreach($sentenceList as $sentence){
				$hay = $sentence -> content;
				$sentence_id = $sentence -> id;
				if ($debug2)	echo "sentence=$hay</br>";
				$curPos=0;
				$cur_occur_no=0;
				$hayLen=mb_strlen($hay);
				do{
					$curFoundPos = mb_strpos($hay,$needle,$curPos);
					if ($curFoundPos===FALSE){
						break;
					}
					
					if ($checkFoundInBig){
						//construct $bigSqls
						$bigSqls = array();
						$bigSqlChineseKeyClause = "chinese_key like \"%$needle%\" and chinese_key<>\"$needle\"";
						if ($bigSql1)
							$bigSqls []= "select chinese_key from term, term_detail where term.id=term_detail.term_id and sentence_id=$sentence_id and term_detail.type<100 and $bigSqlChineseKeyClause";
						if ($option=="极乐寺法师提供的7-8册术语，在4-6册出现，但是没有标记为术语")
							$bigSqls []="select chinese_key from $termTableName, $termDetailTableName where $termTableName.id=$termDetailTableName.term_id and sentence_id=$sentence_id $bigSqlChineseKeyClause";
						if ($bigSql2)
							$bigSqls []="select chinese_key from $termTableName where demo_id<99999 and $bigSqlChineseKeyClause"; 
						
						if ($debug2)	echo "enter look in big</br>";
						$foundInBig = false;
						foreach($bigSqls as $bigSql){
							//if ($debug2)	echo "$bigSql</br>";
							$tempBigs = term::select($bigSql);
							if (empty($tempBigs))
								continue;
							//if ($debug2)	var_dump($tempBigs);
							foreach($tempBigs as $tempBig){
								$bigNeedle = $tempBig->chinese_key;
								//echo "$bigNeedle</br>";
								$smallInBigPos = mb_strpos($bigNeedle,$needle);
								$bigTermPosInSentence = $curFoundPos - $smallInBigPos;
								//echo "$bigTermPosInSentence</br>";
								if ($bigTermPosInSentence<0)
									continue;
								$bigPieceInSentence = mb_substr($hay,$bigTermPosInSentence,mb_strlen($bigNeedle));
								//echo "$bigPieceInSentence</br>";
								if ($bigPieceInSentence==$bigNeedle){
									if ($debug2)	echo "Big found! $needle contained in $bigNeedle</br>";
									$foundInBig=true;
									break;
								}
							}
							flush();ob_flush();
							if ($foundInBig)
								break;
						}
						if ((!$foundInBig) && ($option=="极乐寺法师提供的7-8册术语，在4-6册出现，但是没有标记为术语")){
							file_put_contents($outputFileName,"$needle|$hay\r\n",8);
						}
					}

					if ((!$checkFoundInBig)
						|| (($checkFoundInBig) && (!$foundInBig)))
					{
						if ($fillMode==0){
							$insertUpdateSql = "insert INTO $termDetailTableName (chinese_pos, occur_no, sentence_id, article_id, term_id) VALUES (".
									implode(",",array($curFoundPos,$cur_occur_no,$sentence->id,$sentence->article_id,$term->id)).")";
						}else{
							$sql = "select id, chinese_pos from $termDetailTableName where sentence_id={$sentence->id} and occur_no=$cur_occur_no and term_id={$term->id}";
							if ($debug1)	echo "$sql</br>";
							$temp_r = term_detail::select($sql);
							if (!empty($temp_r)){
								//check
								//echo "existing</br>";
								$term_detail = $temp_r[0];
								if ($term_detail->chinese_pos == $curFoundPos){
									if ($debug1)	echo "existing is correct</br>";
									$insertUpdateSql = "";
								} else{
									if ($debug1)	echo "existing is WRONG</br>";
									$insertUpdateSql = "update $termDetailTableName SET chinese_pos=$curFoundPos where id={$term_detail->id}";
								}
							}
							else{
								//echo "not existed</br>";
								$insertUpdateSql = "insert INTO $termDetailTableName (chinese_pos, occur_no, sentence_id, article_id, term_id) VALUES (".
								implode(",",array($curFoundPos,$cur_occur_no,$sentence->id,$sentence->article_id,$term->id)).")";
								if ($sentence->article_id<=28)	echo "HIHIHIHI</br>";
							}
						}
						
						if($dryRun){
							if ($debug1)	echo "$hay</br>";
							if ($debug1)	echo "$insertUpdateSql</br>";
						}else{
							if (!empty($insertUpdateSql)){
								if ($debug1)	echo "$insertUpdateSql</br>";
								term_detail::select($insertUpdateSql);
								if ($debug1)	echo "inserted</br>";
							}
						}
					}
					$curPos=$curFoundPos+1;
					$cur_occur_no+=1;
				}while($curPos<$hayLen);
			}
		}
    }
*/	
/* old version 1 but workable	
    public function fillTermDetail()
    {
		set_time_limit(0);
		$option = 2; //1:指定$chinese_key_list 2:第4册;
		switch($option){
			case 1:
				$chinese_key_list = array("善知识");
				foreach($chinese_key_list as $chinese_key){
					$chinese_key_list_sql []= "\"".$chinese_key."\"";
				}
				$sql = "select id, chinese_key from term where chinese_key IN (".implode(",",$chinese_key_list_sql).")";
				break;
				
			case 2:
				$sql = "select id, chinese_key from term where length(chinese_key)>3";
				break;
		}
		echo "sql=$sql</br>";
		$termList = term::select($sql);
		
		//Chinese part
		if ($option==2){
			$sql = "delete from term_detail where article_id>=18 and article_id<=28";
			term_detail::select($sql);
		}
		
		foreach($termList as $term){
			echo "id=".$term->id."chinese_key=".$term->chinese_key."</br>";
			if ($option==1){
				$sql = "delete from term_detail where term_id=".$term->id;
				term_detail::select($sql);
			}
			
			$needle = $term->chinese_key;
			switch($option){
				case 1:
					$sql = "select id, article_id, content from sentence";
					break;
				
				case 2:
					$sql = 'select sentence.id, article_id, sentence.content from sentence,article where article.id=sentence.article_id and article.name like "文集6-%"';
					break;
			}
			
			$sentenceList = sentence::select($sql);
			foreach($sentenceList as $sentence){
				$hay = $sentence -> content;
				//echo "sentence=$hay</br>";
				$curPos=0;
				$cur_occur_no=0;
				$hayLen=mb_strlen($hay);
				do{
					$curFoundPos = mb_strpos($hay,$needle,$curPos);
					if ($curFoundPos===FALSE){
						break;
					}
					echo "***curFoundPos=$curFoundPos  ".mb_substr($hay,$curFoundPos)."</br>";	
					if ($cur_occur_no>=2){
						echo "*****cur_occur_no=$cur_occur_no</br>";
					}
					$sql = "insert INTO term_detail (chinese_pos, occur_no, sentence_id, article_id, term_id) VALUES (".
						           implode(",",array($curFoundPos,$cur_occur_no,$sentence->id,$sentence->article_id,$term->id)).")";
					echo "$sql <br>";
					term_detail::insert($sql);
					$curPos=$curFoundPos+1;
					$cur_occur_no+=1;
				}while($curPos<$hayLen);
			}
		}
		
		//var_dump($r);
//        exit(json_encode($r));
    }
*/	
    public function checkTermDetail()
    {
		$sql = "select chinese_pos, sentence_id,term.chinese_key,content,term_detail.id from term, sentence, term_detail where term_detail.term_id=term.id and term_detail.sentence_id=sentence.id and term_detail.article_id between 4 and 28";
		//$sql .= " and term_detail.id>=34643";
		$fix = false;
		$termDetailList = term_detail::select($sql);
		foreach($termDetailList as $v){
			$chinese_key_len = mb_strlen($v->chinese_key);
			$extracted_key = mb_substr($v->content,$v->chinese_pos,$chinese_key_len);
			echo "term_detail_id=".$v->id."  ".(($v->chinese_key==$extracted_key)?"OK":"BAD")."</br>";
			
			if ($v->chinese_key<>$extracted_key){
				echo "{$v->content}</br>{$v->chinese_key}</br>{$v->chinese_pos}</br>";
				if ($fix){
					$this::fillSingleTermDetail($v->id);
					$checkResult = $this::checkTermDetailById($v->id);
					if ($checkResult){
						echo "now ok</br>";
					}else{
						echo "still BAD</br>";
					}
				}
			}
		}
	}
	
	public function findMissingTermDetail()//先不考虑出现多次的情况
	{
		set_time_limit(0);
		$notNeededTerms = array("火灾","本来","所有","如是","夫人","决定","什么","不断","一切","这个","这里","非常","大学");
		$termSql = "select * from term where (source like \"%DFB%\" or source like \"%wj%\") and length(chinese_key)>3";
		$sentenceSql = "select * from sentence where article_id between 4 and 28";
		$terms = term::select($termSql);
		$sentences = sentence::select($sentenceSql);
		foreach($terms as $term){
			if (in_array($term->chinese_key,$notNeededTerms))
				continue;
			foreach($sentences as $sentence){
				$found = true;
				if (mb_strpos($sentence->content,$term->chinese_key)===false)
					continue;
				$termDetailSql = "select * from term_detail where term_id={$term->id} and sentence_id={$sentence->id}";
				$term_details = term_detail::select($termDetailSql);
				if (empty($term_details)){
					$found = false;
					if (!empty($term->contained_in)){
						$containingTermSql = "select * from term where id in ({$term->contained_in})";
						$containing_terms = term::select($containingTermSql);
						foreach($containing_terms as $containing_term){
							$temp_r =  term_detail::select("select * from term_detail where term_id={$containing_term->id} and sentence_id={$sentence->id}");
							if (!empty($temp_r)){
								$found=true;
								break;
							}
						}
					} 
				} else{
					$found = true;
				}
				if (!$found){
					echo "Error not found term={$term->id},{$term->chinese_key} ,{$sentence->id},{$sentence->content}</br>";
				}
			}
		}
	}
	
    public function checkTermDetailById($termDetailIdToCheck)
    {
		$sql = "select chinese_pos, sentence_id,term.chinese_key,content,term_detail.id from term, sentence, term_detail where term_detail.term_id=term.id and term_detail.sentence_id=sentence.id and term_detail.id=$termDetailIdToCheck";
		$termDetailList = term_detail::select($sql);
		if (empty($termDetailList))
			return false;
		foreach($termDetailList as $v){
			$chinese_key_len = mb_strlen($v->chinese_key);
			$extracted_key = mb_substr($v->content,$v->chinese_pos,$chinese_key_len);
			if ($v->chinese_key!=$extracted_key)
				return false;
			//echo "term_detail_id=".$v->id."  ".(($v->chinese_key==$extracted_key)?"OK":"BAD")."</br>";
		}
		return true;
	}
	
	public function fillSingleTermDetail($termDetailId){
		$temp_r = term_detail::select("select content, occur_no, sentence_id, chinese_key, term_detail.id term_detail_id from term_detail, term, sentence where term_detail.term_id=term.id and term_detail.sentence_id and term_detail.id={$termDetailId}");
		if (empty($termDetailId)){
			echo "Error $termDetailId NOT found</br>";
			return false;
		}
		
		$content = $temp_r[0]->content;
		$chinese_key = $temp_r[0]->chinese_key;	
		$occur_no = $temp_r[0]->occur_no;
		
		$startPos = -1;
		for($i=0;$i<$occur_no;$i++){
			$startPos = mb_strpos($content,$chinese_key,$startPos+1);
			if ($startPos===false){
				echo "Error occur $i NOT found</br>";
				return false;
			}
		}
		term_detail::select("update term_detail SET chinese_pos=$startPos where id={$termDetailId}");
		return true;
	}
	
    public function studyTermDetail()
    {
		$sql = "select term.chinese_key,term.source,count(*) mycount from term_detail, sentence, term where term_detail.article_id>=4 and term_detail.sentence_id=sentence.id and term_detail.term_id=term.id group by term_detail.term_id";
		$termDetailList = term_detail::select($sql);
		foreach($termDetailList as $termDetail){
			echo $termDetail->chinese_key."|".$termDetail->source."|".$termDetail->mycount."</br>";
		}
	}
	
    public function fillTermDetailEng()
	{
		$chinese_key_list = array("佛法"); //善知识;发心;"共业","比丘"
		foreach($chinese_key_list as $chinese_key){
			$chinese_key_list_sql []= "\"".$chinese_key."\"";
		}
		
		$articleSql = " AND ( (term_detail.article_id>=4 and term_detail.article_id<=10) OR (term_detail.article_id>=18 and term_detail.article_id<=28))"; //default:""
		$specifiedEnglishList = array(); //array("one's rise to thought","goodwill"); //default:array()
		$updateAction = true; //default:false
		$onlyEmptyEnglishPos = true;
		$englishPosSql = ($onlyEmptyEnglishPos) ? " AND term_detail.english_pos IS NULL" : "";
		$termDetailIdSql = "";//" AND term_detail.id IN (5064,5065)"; //default:""
		
		//English part
		foreach($chinese_key_list_sql as $chinese_key_sql){
			$sql = "select id, chinese_key, english_all from term where chinese_key =$chinese_key_sql";
			$termInfo = term::select($sql);
			$termInfo = $termInfo[0];
			if (empty($specifiedEnglishList))
				$englishList = explode("[e]",$termInfo->english_all);
			else
				$englishList = $specifiedEnglishList;
			
			//var_dump($englishList);
			
			$sql = "select sentence.content, sentence.translation, term_detail.id term_detail_id, english, english_pos,term_detail.occur_no from sentence, term_detail where sentence.id=term_detail.sentence_id AND term_id =".$termInfo->id.$articleSql.$englishPosSql.$termDetailIdSql;
			//$sql = 'select sentence.translation, term_detail.id term_detail_id, english_pos from sentence, term_detail where sentence.id=term_detail.sentence_id AND term_id ='.$termInfo->id.' AND sentence.translation like "%triv%"';
			$termDetailList = (term_detail::select($sql));
			//echo "$sql</br>";var_dump($termDetailList);
			foreach($termDetailList as $termDetail){
				$curSentence = $termDetail->translation;
				if (strlen($curSentence)<8)
					continue;
				
				/* 只输出english_pos为空的中英文例句 
				if (empty(trim($termDetail->english_pos))){
					echo "{$termDetail->term_detail_id}</br>{$termDetail->content}</br>$curSentence</br>{$termDetail->english_pos}</br>";
				} 
				continue;
				*/	
				
				$curSentenceOccurNoPassed = true;
				$fromWordInSentence = 0;
				$start_len_arr = array();
				$matchedEnglishTerm = "";
				//echo "occur_no={$termDetail->occur_no}</br>";
				for($occur_no_i=0;$occur_no_i<$termDetail->occur_no;$occur_no_i++){
					//echo "occur_no_i={$occur_no_i}</br>";
					$start_len_arr = array();
					foreach($englishList as $englishTerm){
						$r = pos_in_sentence($englishTerm, $curSentence,$fromWordInSentence);
						if(!empty($r)){
							//echo "***found</br>";
							//echo $curSentence."</br>";
							$lastStartWordPos = 0;
							foreach($r as $k=>$v){
								$start_len_arr []= $v['start'].",".$v['len'];
								$lastStartWordPos = $v['startWordPos'];
							}
							//var_dump($r);
							break;
						}
					}
					if (empty($start_len_arr)){
						$curSentenceOccurNoPassed = false;
						break;
					}
					
					if ($occur_no_i==$termDetail->occur_no){
						$matchedEnglishTerm = $englishTerm;
						//echo "matchedEnglishTerm=$matchedEnglishTerm</br>";
						break; //列表里在前面的术语优先匹配
					}
					
					$fromWordInSentence = $lastStartWordPos + 1;
				}
				if ($curSentenceOccurNoPassed == true){
					$start_len = implode(";",$start_len_arr);
					echo "{$termDetail->term_detail_id}  $start_len</br>";
					$sql = 'update term_detail SET `english`="'.$matchedEnglishTerm.'", `english_pos`="'.$start_len.'" where id='.$termDetail->term_detail_id;	
					if ($updateAction)
						term_detail::select($sql);
					else
						;//echo "$sql</br>";
				} else {
					echo "curSentence=$curSentence</br>***not found</br>";
				}
			}
		}
	}	
		
	public function searchEmptyTermDetailEng(){
		$sql = 'select term_detail.english,sentence.content,sentence.translation,sentence.seq_no, article.name from article,term_detail, sentence,term where term_detail.article_id>=11 and term_detail.article_id<=17 and term_detail.sentence_id=sentence.id and term_detail.article_id=article.id and term_detail.term_id=term.id and term.chinese_key="佛法"';
		$r = term_detail::select($sql);
		foreach($r as $term_detail){
			if(mb_strlen($term_detail->english)<3){
				//echo $term_detail->name."</br>".$term_detail->content."</br>".$term_detail->translation."</br></br>----------------------------</br>";
				file_put_contents("\lishu_book4_buddhadharma.txt",$term_detail->name."|".$term_detail->seq_no."|".$term_detail->content."|".$term_detail->translation."\r\n",8);
			}
		}
	}
	
	public function exportBook(){
		$chinese_key_list = array("境界","因缘");
		foreach($chinese_key_list as $chinese_key){
			$chinese_key_list_sql []= "\"".$chinese_key."\"";
		}
		$sql = "select id, chinese_key from term where chinese_key IN (".implode(",",$chinese_key_list_sql).")";
		$terms = term::select($sql);
		
		foreach($terms as $term){
			$sql = "select sentence.content,sentence.translation,sentence.seq_no, article.name from article,sentence,term_detail where sentence.article_id>=4 and sentence.article_id<=28 and sentence.article_id=article.id and term_detail.sentence_id=sentence.id and term_detail.term_id={$term->id} order by sentence.article_id, sentence.seq_no"; //11-17文集4
			$r = sentence::select($sql);
			foreach($r as $sentence){
				file_put_contents("\lishu_test.txt",$sentence->name."|".$sentence->seq_no."|".$sentence->content."|".$sentence->translation."|".$term->chinese_key."\r\n",8);
			}
		}
	}
	
    public function addSentenceSeqNo()
    {
		$sql = "select id from article";
		$articleList = article::select($sql);
		foreach($articleList as $v){
			$cur_seq_no = 1;
			$sql = "select id from sentence where article_id=".$v->id." order by id";
			$sentenceList = sentence::select($sql);
			foreach($sentenceList as $v2){
				$sql = "update sentence set seq_no=".$cur_seq_no." where id=".$v2->id;
				$sentenceList = sentence::update($sql);
				$cur_seq_no ++ ;
				if ($cur_seq_no % 10 == 0 ){
					echo "update article_id=".$v->id." sentence_id=".$v2->id."<br>";
				}
			}
		}
		echo "done</br>";
	}
	
	public function calcFreq(){
		set_time_limit(0);
		$fileNamePartArr = array(
			"01fwyrs-01","01fwyrs-02","01fwyrs-03","01fwyrs-04",
			"02jwzd-01","02jwzd-02","02jwzd-03","02jwzd-04","02jwzd-05",
			"03qfptx-01","03qfptx-02","03qfptx-03",
			"04gwrs-01","04gwrs-02","04gwrs-03","04gwrs-04","04gwrs-05","04gwrs-06","04gwrs-07","04gwrs-08","04gwrs-09","04gwrs-10",
			"05klrs-01","05klrs-02","05klrs-03","05klrs-04","05klrs-05","05klrs-06","05klrs-07","05klrs-08","05klrs-09","05klrs-10","05klrs-11","05klrs-12",
			"06rsrs-01","06rsrs-02","06rsrs-03","06rsrs-04","06rsrs-05","06rsrs-06","06rsrs-07","06rsrs-08","06rsrs-09","06rsrs-10",
			"07jxgy-01","07jxgy-02","07jxgy-03","07jxgy-04","07jxgy-05","07jxgy-06",
			"08gyzl-01","08gyzl-02","08gyzl-03","08gyzl-04","08gyzl-05","08gyzl-06","08gyzl-07","08gyzl-08","08gyzl-09","08gyzl-10",
			"08gyzl-11","08gyzl-12","08gyzl-13","08gyzl-14","08gyzl-15","08gyzl-16","08gyzl-17","08gyzl-18",
			"09zzks-01","09zzks-02","09zzks-03","09zzks-04","09zzks-05","09zzks-06","09zzks-07","09zzks-08","09zzks-09","09zzks-10",
			"09zzks-11","09zzks-12","09zzks-13","09zzks-14","09zzks-15","09zzks-16","09zzks-17","09zzks-18","09zzks-19","09zzks-20",
			"09zzks-21","09zzks-22","09zzks-23","09zzks-24","09zzks-25","09zzks-26","09zzks-27",
			"10jtzx-01","10jtzx-02","10jtzx-03","10jtzx-04","10jtzx-05","10jtzx-06","10jtzx-07","10jtzx-08","10jtzx-09","10jtzx-10","10jtzx-11","10jtzx-12","10jtzx-13","10jtzx-14","10jtzx-15","10jtzx-16","10jtzx-17","10jtzx-18","10jtzx-19",
			"11pbsn-01","11pbsn-02","11pbsn-03","11pbsn-04","11pbsn-05","11pbsn-06","11pbsn-07","11pbsn-08","11pbsn-09","11pbsn-10",
			"12xdsj-01","12xdsj-02","12xdsj-03","12xdsj-04","12xdsj-05","12xdsj-06","12xdsj-07","12xdsj-08","12xdsj-09","12xdsj-10","12xdsj-11","12xdsj-12","12xdsj-13","12xdsj-14","12xdsj-15","12xdsj-16","12xdsj-17","12xdsj-18","12xdsj-19",
			"13fjjs-01","13fjjs-02","13fjjs-03","13fjjs-04","13fjjs-05","13fjjs-06","13fjjs-07","13fjjs-08","13fjjs-09","13fjjs-10","13fjjs-11","13fjjs-12","13fjjs-13","13fjjs-14","13fjjs-15","13fjjs-16","13fjjs-17","13fjjs-18","13fjjs-19","13fjjs-20","13fjjs-21","13fjjs-22","13fjjs-23","13fjjs-24","13fjjs-25","13fjjs-26","13fjjs-27","13fjjs-28","13fjjs-29","13fjjs-30","13fjjs-31","13fjjs-32",
			"14scpy-01","14scpy-02","14scpy-03","14scpy-04","14scpy-05","14scpy-06","14scpy-07","14scpy-08","14scpy-09","14scpy-10","14scpy-11","14scpy-12","14scpy-13",
			"15hdks-01","15hdks-02","15hdks-03","15hdks-04","15hdks-05","15hdks-06","15hdks-07","15hdks-08","15hdks-09","15hdks-10","15hdks-11","15hdks-12","15hdks-13","15hdks-14","15hdks-15","15hdks-16","15hdks-17","15hdks-18","15hdks-19","15hdks-20","15hdks-21","15hdks-22",
			"16xwh-01","16xwh-02","16xwh-03","16xwh-04","16xwh-05","16xwh-06","16xwh-07","16xwh-08","16xwh-09","16xwh-10","16xwh-11","16xwh-12","16xwh-13","16xwh-14","16xwh-15","16xwh-16","16xwh-17","16xwh-18","16xwh-19","16xwh-20","16xwh-21","16xwh-22","16xwh-23","16xwh-24","16xwh-25",
			"17dwl-01dcp-01","17dwl-01dcp-02","17dwl-01dcp-03","17dwl-01dcp-04","17dwl-01dcp-05","17dwl-01dcp-06","17dwl-01dcp-07","17dwl-01dcp-08","17dwl-01dcp-09","17dwl-01dcp-11","17dwl-01dcp-13","17dwl-01dcp-14","17dwl-01dcp-15","17dwl-01dcp-16","17dwl-01dcp-17","17dwl-01dcp-18","17dwl-01dcp-19","17dwl-01dcp-20","17dwl-02gxp-01","17dwl-02gxp-02","17dwl-02gxp-03","17dwl-02gxp-04","17dwl-02gxp-05","17dwl-02gxp-06","17dwl-02gxp-07","17dwl-02gxp-08","17dwl-02gxp-09","17dwl-02gxp-10","17dwl-02gxp-11","17dwl-02gxp-12","17dwl-02gxp-13","17dwl-02gxp-14","17dwl-02gxp-15","17dwl-02gxp-16","17dwl-02gxp-17","17dwl-03shp-01","17dwl-03shp-02","17dwl-03shp-03","17dwl-03shp-04","17dwl-03shp-05","17dwl-03shp-06",
			"18mtcf-01","18mtcf-02","18mtcf-03","18mtcf-04","18mtcf-05","18mtcf-06","18mtcf-07","18mtcf-08","18mtcf-09","18mtcf-10","18mtcf-11","18mtcf-12","18mtcf-13","18mtcf-14","18mtcf-15","18mtcf-16","18mtcf-17","18mtcf-18"
		);
		$sql = 'select id,chinese_key, source from term where id=28572 order by id '; //
		$termArr = term::select($sql);
		
		foreach($termArr as $term){
			$subtotal = 0;
			$termInfo = $term->chinese_key . "|" .
						((strpos($term->source,"DFB")===FALSE) ? "N" : "Y" ). "|" .
						((strpos($term->source,"wj_5")===FALSE) ? "N" : "Y" ). "|" .
						((strpos($term->source,"liuxl_0223")===FALSE) ? "N" : "Y" ). "|" .
						((strpos($term->source,"wanglz_0221")===FALSE) ? "N" : "Y" );
			echo $term->id."</br>";
			//echo $term->source."  ".$termInfo."</br>";			
			foreach($fileNamePartArr as $fileNamePart){
				$fileName = "F:\\000_booktran\\18series\\".$fileNamePart.".txt";
				$file = file_get_contents($fileName);
				$curCount = $this->mb_count_substr($file,$term->chinese_key);
				$subtotal += $curCount;
				file_put_contents("\lishu_detail.txt",$termInfo."|".$curCount."|".$fileNamePart."|"."\r\n",8);
			}
			file_put_contents("\lishu_total.txt",$termInfo."|".$subtotal."\r\n",8);
			flush();ob_flush();
		}
		
	}
	
	public function mb_count_substr($hay,$needle){
		$curPos=0;
		$cur_occur_no=0;
		$hayLen=mb_strlen($hay);
		do{
			$curFoundPos = mb_strpos($hay,$needle,$curPos);
			if ($curFoundPos===FALSE){
				break;
			}
			//echo "***curFoundPos=$curFoundPos  ".mb_substr($hay,$curFoundPos)."</br>";	
			if ($cur_occur_no>=2){
				//echo "*****cur_occur_no=$cur_occur_no</br>";
			}
			/*
			$sql = "insert INTO term_detail (chinese_pos, occur_no, sentence_id, article_id, term_id) VALUES (".
				           implode(",",array($curFoundPos,$cur_occur_no,$sentence->id,$sentence->article_id,$term->id)).")";
			echo "$sql <br>";
			term_detail::insert($sql);*/
			$curPos=$curFoundPos+1;
			$cur_occur_no+=1;
		}while($curPos<$hayLen);
		return $cur_occur_no;
	}			
	
	public function getSentence(){
		set_time_limit(0);
		$termSource = 1;
		
		$fileNamePartArr = $this::get18SeriesFileName();
		$fileChnNamePartArr = $this::get18SeriesChnFileName($fileNamePartArr);
		
		if ($termSource==0){
			$termFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\18series\\18修订稿（终稿）-export.csv");
			$termFile = file_get_contents("$termFileName");
			$termLines = explode("\r\n",$termFile);
			
			$termArr=array();
			foreach($termLines as $k=>$termLine){
				if (($k<=655) || mb_strlen($termLine)<=1)
					continue;
				$termArr [] = array('chinese_key'=>$termLine,'seq_no'=>$k);
			}
		}else{
			//$termArr = array(
			//	array('chinese_key'=>'三宝','seq_no'=>3230),
			//	array('chinese_key'=>'佛法僧三宝','seq_no'=>3231),
			//	array('chinese_key'=>'佛法僧','seq_no'=>3232)
			//	);
			$termArr = array(array('chinese_key'=>'身安乐行','seq_no'=>3233));
		}
		
		foreach($termArr as $k=>$term){
			
			//$outputFileName = sprintf("\\term_sentence_%04u-%04u.txt",floor($k/20)*20+1,floor($k/20)*20+20);
			$outputFileName = sprintf("\\%04u-%s.txt",$term['seq_no'],iconv('UTF-8','GB2312',str_replace("涅槃","niepan",$term['chinese_key'])));
			echo $outputFileName;
			//echo "术语".$k."：".$term['chinese_key'].",  频次：".$term['frequency']."\r\n";
			$k1=$k+1;
			//file_put_contents($outputFileName,"术语".$k1 ."：".$term['chinese_key'].",  频次：".$term['frequency']."\r\n",8);
			
			$sentenceIdx=1;
			foreach($fileNamePartArr as $fIdx=>$fileNamePart){
				$fileName = "F:\\000_booktran\\18series\\ShifuTeachings\\".$fileNamePart.".txt";
				//echo $fileName."<br>";
				$file = file_get_contents($fileName);
				
				$lines= explode("\r\n",$file);
				foreach($lines as $line){
					if (mb_strlen($line)<=1)
						continue;
					if (mb_strpos($line,$term['chinese_key'])!==FALSE){ 
						file_put_contents($outputFileName,sprintf("例句%u：\r\n出处：《%s》\r\n",$sentenceIdx,$fileChnNamePartArr[$fIdx]).$line."\r\n\r\n",8);
						$sentenceIdx++;
					}
				}
			}
			//file_put_contents($outputFileName,"=========================================================\r\n",8);
			
			$sql = 'select sentence.content, sentence.translation, article_id, article.name from sentence, article where sentence.content like "%'.$term['chinese_key'].'%" AND article_id>=4 and article_id<=28 and sentence.article_id=article.id';
			echo "$sql</br>";
			$sentences = sentence::select($sql);
			foreach($sentences as $sentence){
				file_put_contents($outputFileName,sprintf("例句%u：\r\n出处：《%s》\r\n%s\r\n%s\r\n\r\n",$sentenceIdx,$sentence->name,$sentence->content,$sentence->translation),8);
				$sentenceIdx++;
			}
			flush();ob_flush();
		}
		
	}	
	
	public function get18SeriesFileName(){
		return array(
			"01fwyrs-01","01fwyrs-02","01fwyrs-03","01fwyrs-04",
			"02jwzd-01","02jwzd-02","02jwzd-03","02jwzd-04","02jwzd-05",
			"03qfptx-01","03qfptx-02","03qfptx-03",
			"04gwrs-01","04gwrs-02","04gwrs-03","04gwrs-04","04gwrs-05","04gwrs-06","04gwrs-07","04gwrs-08","04gwrs-09","04gwrs-10",
			"05klrs-01","05klrs-02","05klrs-03","05klrs-04","05klrs-05","05klrs-06","05klrs-07","05klrs-08","05klrs-09","05klrs-10","05klrs-11","05klrs-12",
			"06rsrs-01","06rsrs-02","06rsrs-03","06rsrs-04","06rsrs-05","06rsrs-06","06rsrs-07","06rsrs-08","06rsrs-09","06rsrs-10",
			"07jxgy-01","07jxgy-02","07jxgy-03","07jxgy-04","07jxgy-05","07jxgy-06",
			"08gyzl-01","08gyzl-02","08gyzl-03","08gyzl-04","08gyzl-05","08gyzl-06","08gyzl-07","08gyzl-08","08gyzl-09","08gyzl-10",
			"08gyzl-11","08gyzl-12","08gyzl-13","08gyzl-14","08gyzl-15","08gyzl-16","08gyzl-17","08gyzl-18",
			"09zzks-01","09zzks-02","09zzks-03","09zzks-04","09zzks-05","09zzks-06","09zzks-07","09zzks-08","09zzks-09","09zzks-10",
			"09zzks-11","09zzks-12","09zzks-13","09zzks-14","09zzks-15","09zzks-16","09zzks-17","09zzks-18","09zzks-19","09zzks-20",
			"09zzks-21","09zzks-22","09zzks-23","09zzks-24","09zzks-25","09zzks-26","09zzks-27",
			"10jtzx-01","10jtzx-02","10jtzx-03","10jtzx-04","10jtzx-05","10jtzx-06","10jtzx-07","10jtzx-08","10jtzx-09","10jtzx-10","10jtzx-11","10jtzx-12","10jtzx-13","10jtzx-14","10jtzx-15","10jtzx-16","10jtzx-17","10jtzx-18","10jtzx-19",
			"11pbsn-01","11pbsn-02","11pbsn-03","11pbsn-04","11pbsn-05","11pbsn-06","11pbsn-07","11pbsn-08","11pbsn-09","11pbsn-10",
			"12xdsj-01","12xdsj-02","12xdsj-03","12xdsj-04","12xdsj-05","12xdsj-06","12xdsj-07","12xdsj-08","12xdsj-09","12xdsj-10","12xdsj-11","12xdsj-12","12xdsj-13","12xdsj-14","12xdsj-15","12xdsj-16","12xdsj-17","12xdsj-18","12xdsj-19",
			"13fjjs-01","13fjjs-02","13fjjs-03","13fjjs-04","13fjjs-05","13fjjs-06","13fjjs-07","13fjjs-08","13fjjs-09","13fjjs-10","13fjjs-11","13fjjs-12","13fjjs-13","13fjjs-14","13fjjs-15","13fjjs-16","13fjjs-17","13fjjs-18","13fjjs-19","13fjjs-20","13fjjs-21","13fjjs-22","13fjjs-23","13fjjs-24","13fjjs-25","13fjjs-26","13fjjs-27","13fjjs-28","13fjjs-29","13fjjs-30","13fjjs-31","13fjjs-32",
			"14scpy-01","14scpy-02","14scpy-03","14scpy-04","14scpy-05","14scpy-06","14scpy-07","14scpy-08","14scpy-09","14scpy-10","14scpy-11","14scpy-12","14scpy-13",
			"15hdks-01","15hdks-02","15hdks-03","15hdks-04","15hdks-05","15hdks-06","15hdks-07","15hdks-08","15hdks-09","15hdks-10","15hdks-11","15hdks-12","15hdks-13","15hdks-14","15hdks-15","15hdks-16","15hdks-17","15hdks-18","15hdks-19","15hdks-20","15hdks-21","15hdks-22",
			"16xwh-01","16xwh-02","16xwh-03","16xwh-04","16xwh-05","16xwh-06","16xwh-07","16xwh-08","16xwh-09","16xwh-10","16xwh-11","16xwh-12","16xwh-13","16xwh-14","16xwh-15","16xwh-16","16xwh-17","16xwh-18","16xwh-19","16xwh-20","16xwh-21","16xwh-22","16xwh-23","16xwh-24","16xwh-25",
			"17dwl-01dcp-01","17dwl-01dcp-02","17dwl-01dcp-03","17dwl-01dcp-04","17dwl-01dcp-05","17dwl-01dcp-06","17dwl-01dcp-07","17dwl-01dcp-08","17dwl-01dcp-09","17dwl-01dcp-11","17dwl-01dcp-13","17dwl-01dcp-14","17dwl-01dcp-15","17dwl-01dcp-16","17dwl-01dcp-17","17dwl-01dcp-18","17dwl-01dcp-19","17dwl-01dcp-20","17dwl-02gxp-01","17dwl-02gxp-02","17dwl-02gxp-03","17dwl-02gxp-04","17dwl-02gxp-05","17dwl-02gxp-06","17dwl-02gxp-07","17dwl-02gxp-08","17dwl-02gxp-09","17dwl-02gxp-10","17dwl-02gxp-11","17dwl-02gxp-12","17dwl-02gxp-13","17dwl-02gxp-14","17dwl-02gxp-15","17dwl-02gxp-16","17dwl-02gxp-17","17dwl-03shp-01","17dwl-03shp-02","17dwl-03shp-03","17dwl-03shp-04","17dwl-03shp-05","17dwl-03shp-06",
			"18mtcf-01","18mtcf-02","18mtcf-03","18mtcf-04","18mtcf-05","18mtcf-06","18mtcf-07","18mtcf-08","18mtcf-09","18mtcf-10","18mtcf-11","18mtcf-12","18mtcf-13","18mtcf-14","18mtcf-15","18mtcf-16","18mtcf-17","18mtcf-18"
		);
		
	}
	
	public function get18SeriesChnFileName($fileNamePartArr){
		$fileChnNamePartArr = array();
		foreach($fileNamePartArr as $fileName){
			$curName = $fileName;
			$curName = str_replace("01fwyrs","1 佛法与人生",$curName);
			$curName = str_replace("02jwzd", "2 觉悟之道",$curName);
			$curName = str_replace("03qfptx","3 劝发菩提心",$curName);
			$curName = str_replace("04gwrs", "4 感悟人生",$curName);
			$curName = str_replace("05klrs", "5 苦乐人生",$curName);
			$curName = str_replace("06rsrs", "6 认识人生",$curName);
			$curName = str_replace("07jxgy", "7 敬修皈依",$curName);
			$curName = str_replace("08gyzl", "8 皈依之路",$curName);
			$curName = str_replace("09zzks", "9 早斋开示",$curName);
			$curName = str_replace("10jtzx", "10解脱之心-正见",$curName);
			$curName = str_replace("11pbsn", "11 毗钵舍那",$curName);
			$curName = str_replace("12xdsj", "12 心的世界（百法）",$curName);
			$curName = str_replace("13fjjs", "13 佛教建设",$curName);
			$curName = str_replace("14scpy", "14 僧才培养",$curName);
			$curName = str_replace("15hdks", "15 活动开示",$curName);
			$curName = str_replace("16xwh",  "16 心文化",$curName);
			$curName = str_replace("17dwl-01dcp-","17 答问录•道场篇",$curName);
			$curName = str_replace("17dwl-02gxp-","17 答问录•高校篇",$curName);
			$curName = str_replace("17dwl-03shp-","17 答问录•社会篇",$curName);
			$curName = str_replace("18mtcf", "18 媒体采访",$curName);
			$fileChnNamePartArr []= $curName;
		}
		return $fileChnNamePartArr;
	}
	
	public function exportSentence(){
		$sql = 'SELECT article.name, sentence.content, sentence.translation FROM `sentence`,article WHERE (sentence.content like "%持戒%" or sentence.content like "%戒相%" or sentence.content like "%早殿%" or sentence.content like "%晚殿%" or sentence.content like "%早晚殿%") and sentence.article_id=article.id';
		$r = sentence::select($sql);
		foreach($r as $sentence){
				//echo $term_detail->name."</br>".$term_detail->content."</br>".$term_detail->translation."</br></br>----------------------------</br>";
			file_put_contents("\lishu_test.txt",$sentence->name."|".$sentence->content."|".$sentence->translation."\r\n",8);
		}
	}

    public function calcTermContainedIn()
    {
		set_time_limit(0);
		
		$sql = 'SELECT * FROM `term` WHERE (source like "%DFB%" OR source like "%wj%")';
		$terms = term::select($sql);
		foreach($terms as $term){
			$curChineseKey=$term->chinese_key;
			echo "{$term->chinese_key}:";
			$sql = 'SELECT * FROM `term` WHERE chinese_key like "%'.$curChineseKey.'%" and chinese_key<>"'.$curChineseKey.'"';// and (source like "%DFB%" OR source like "%wj_5%")';
			//$sql .= " and category=\"bookname\"";
			$terms2 = term::select($sql);
			if (!empty($terms2)){
				$contained_in_list = array();
				foreach($terms2 as $term2){
					$contained_in_list[] = $term2->id;
				}
				$contained_in = implode(",",$contained_in_list);
				echo $contained_in;
				$sql = 'update `term` SET contained_in = "'.$contained_in.'" where id='.$term->id;
				term::select($sql);
			}
			echo "</br>";
		}
		
		//verify
		$sql = 'SELECT * FROM `term` WHERE contained_in IS NOT NULL and (source like "%DFB%" OR source like "%wj%")';
		$terms = term::select($sql);
		foreach($terms as $term){
			$curChineseKey=$term->chinese_key;
			echo "{$term->chinese_key}:";
			$contained_in_list = explode(",",$term->contained_in);
			foreach($contained_in_list as $contained_in_id){
				$sql = 'SELECT * FROM `term` WHERE id='.$contained_in_id;
				$r = term::select($sql);
				echo $r[0]->chinese_key."  ,";
			}
			echo "</br>";
		}
	}

    public function processContainedIn() //similar to containedIn
    {
		set_time_limit(0);
		$dryRun = true;
		
		$containingSql = "(source like \"%DFB%\" or source like \"%wj%\")";//"category in (\"buddhaname\",\"patriarchname\",\"bookname\")";
		$containingSql2 = "chinese_key=\"修行经验\"";//佛教徒
		$containedSql = " chinese_key=\"修行经\"";
		$sql = 'select term_id, term_detail.id term_detail_id, sentence_id, chinese_key,chinese_pos,contained_in,sentence.content from term,term_detail,sentence where term.id=term_detail.term_id and term_detail.sentence_id=sentence.id and contained_in IS NOT NULL and term_detail.article_id between 4 and 28 and '.$containedSql;
		$term_details=term_detail::select($sql);
		echo "term_id={$term_details[0]->term_id}</br>";
		foreach($term_details as $term_detail){
			//if ($term_detail->term_detail_id)echo "{$term_detail->chinese_key}</br>";
			$chinese_pos = $term_detail->chinese_pos;
			$len = mb_strlen($term_detail->chinese_key);
			
			$temp_contained = term_detail::select("select count(id) count from term_detail where sentence_id={$term_detail->sentence_id} and term_id={$term_detail->term_id} group by sentence_id");
			if (empty($temp_contained))
				continue;
			$contained_count = $temp_contained[0]->count;
			
			$sql = "select term_id, chinese_key,chinese_pos, term_detail.id term_detail_id from term, term_detail where term.id=term_detail.term_id and sentence_id={$term_detail->sentence_id} and term_id in ({$term_detail->contained_in}) and $containingSql and $containingSql2";
			//echo "sql=$sql</br>";
			$containing_term_details = term_detail::select($sql);
			//var_dump($containing_term_details);
			foreach($containing_term_details as $containing_term_detail){
				$containing_chinese_pos = $containing_term_detail->chinese_pos;
				if ($chinese_pos-$containing_chinese_pos === mb_strpos($containing_term_detail->chinese_key, $term_detail->chinese_key)){
					ob_flush();flush();
					$temp_containing = term_detail::select("select count(id) count from term_detail where sentence_id={$term_detail->sentence_id} and term_id={$containing_term_detail->term_id} group by sentence_id");
					$containing_count = $temp_containing[0]->count;
					//echo "contained_count=$$contained_count containing_count=$containing_count</br>";
					if ($contained_count<>$containing_count){
						echo "Count different: {$term_detail->chinese_key}|{$containing_term_detail->chinese_key}</br> ";
						echo "{$term_detail->content}</br>";
						continue;
					}
					$sql = "delete from term_detail where id=".$term_detail->term_detail_id;
					if (!$dryRun){
						term_detail::select($sql);
					}else{
						echo "$sql</br>";
					}
					//echo "{$term_detail->chinese_key}|{$containing_term_detail->chinese_key}|{$term_detail->content}|$sql</br>";
					//file_put_contents("\\delete.txt","{$term_detail->chinese_key}|{$containing_term_detail->chinese_key}|{$term_detail->content}|$sql\r\n",8);
					break;
				}
			}
		}
	}
	
    public function containedIn()
    {
		set_time_limit(0);
		$sql = 'select id, chinese_key,contained_in from term where contained_in IS NOT NULL';
		$terms=term::select($sql);
		foreach($terms as $term){
			echo "****** term={$term->chinese_key}</br>";
			$contained_in = $term->contained_in;
			//$contained_in_id_list = explode(",",$contained_in);
			$sql = "select id, chinese_key,contained_in from term where id IN ($contained_in) ";
			$contained_in_terms = $terms=term::select($sql);
			foreach($contained_in_terms as $contained_in_term){
				echo "  **** contained_in_term={$contained_in_term->chinese_key}</br>";
				$contained_in_id = $contained_in_term->id;
				if ($contained_in_id<>$term->id){
					//var_dump($contained_in_term);
					$contained_in_chinese_key = trim($contained_in_term->chinese_key);
					$contained_in_chinese_key_len = mb_strlen($contained_in_chinese_key);
					$sql = "select term_detail.id, content,chinese_pos from term_detail,sentence where term_detail.sentence_id=sentence.id and term_id={$term->id} and content like \"%$contained_in_chinese_key%\" ";
					$termDetailList = sentence::select($sql);
					//var_dump($termDetailList);
					foreach($termDetailList as $v){
						echo "  **{$v->content}</br>";
						$contained_in_start = mb_strpos($v->content,$contained_in_chinese_key);//不考虑contained_in_term(大术语)出现多次
						if ($contained_in_start !== FALSE){
							if (($contained_in_start<=$v->chinese_pos) && ($contained_in_start+$contained_in_chinese_key_len>=$v->chinese_pos+mb_strlen($term->chinese_key))){
								//$extracted_key = mb_substr($v->content,$v->chinese_pos,$chinese_key_len);echo "$extracted_key</br>";
								echo "    ok</br>";
								$sql = "delete from term_detail where id=\"{$v->id}\""; //搞反了！！
								term_detail::select($sql);
							}
						}
						echo "</br>";
					}
				}
			}
		}
	}
	
/*
//change to excel, if new version can work, then v1 can be 	deleted
    public function importTermDetail_v1()
    {
		set_time_limit(0);
		$dryRun = true;
		$option="mtg0419";
		//$option="术语提炼-b3-0405-sherry-part3";
		//$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\文集4-佛法-lishu-v3.csv");
		//$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\因缘&境界-杨雪赵悦-20180306-王一晨-李舒.csv");
		//$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\术语提炼-第4-6册（汇总）.csv");
		switch($option){
			case "0404_1-5000":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\1-5000\\术语提炼-第4-6册（汇总）1-5000条审核 by wyn.csv");
				$chinese_key_list = array("修行经验","修行人","修行","修道","修学");
				$format=0;
				break;
			
			case "b3-0403-sherry":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\b3-0403\\术语提炼-b3-0403-output-sherry.txt");
				$chinese_key_list = array("修行者","学佛");
				$format=1;
				break;
				
			case "b3-0403-part2.2":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\b3-0403\\术语提炼-b3-0403-part2.2-correct-output.txt");
				$chinese_key_list = array("学修");
				$format=1;
				break;
				
			case "术语提炼-b3-0405-sherry-part3":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\b3-0403\\术语提炼-b3-0405-sherry-part3-output.txt");
				$chinese_key_list = array("有情","无情","修行者");
				$format=1;
				break;
				
			case "mtg0419":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\句子序号\\术语提炼-第4-6册-句子序号（汇总）.txt");
				$chinese_key_list = array("有情","无情","修行者");
				$format=2;
				break;
		}
		
		switch($format){
			case 0:
				$fieldCount = 5;
				$delimeter = "\r\n";
				$fieldOffset = 0;
				$article_name_index = 0;
				$seq_no_index = 1;
				$chinese_key_index = 2;
				$englishTermIndex = 3;
				$termCommentIndex = 4;
				break;
				
			case 1:
				$fieldCount = 8;
				$delimeter = "\t";
				$fieldOffset = 2;
				$article_name_index = 1;
				$seq_no_index = 2;
				$sentenceCnIdx = 3;
				$sentenceEnIdx = 4;
				$chinese_key_index = 5;
				$englishTermIndex = 6;
				$termCommentIndex = 7;
				break;
				
			case 2:
				$fieldCount = 9;
				$delimeter = "\t";
				$article_name_index = 2;
				$seq_no_index = 3;
				$sentenceCnIdx = 4;
				$sentenceEnIdx = 5;
				$chinese_key_index = 6;
				$englishTermIndex = 7;
				$termCommentIndex = 8;
				break;
		}
		
		if(eregi("\.txt$", $fileName)){
			$fileFormat = "txt";
		}else{
			$fileFormat = "excel";
		}
		
		if ($fileFormat=="txt"){
			$file = file_get_contents("$fileName");
			$lines = explode("\r\n",$file);
		}else{
			$cellData=ExcelActionController::myImportExcelByFileNameSheetIndex($fileName, $sheetIndex);
		}
		
		$terms = $term_types = $term_comments = array();
		$lastSentenceOccurCnt = 0;
		foreach($lines as $k=>$line){
			$fields = explode($delimeter,$line);
			for($i=0;$i<$fieldCount;$i++)
				$fields[$i] = empty($fields[$i]) ? "" : trim($fields[$i]);
			
			//var_dump($fields);continue;
			if (empty($fields)){
				if ($k<($count($lines)-1)){
					echo "error: line $k  为空</br>"; 
				}else{
					echo "最后一行 line $k  为空</br>"; 
				}
				continue;
			}
			$article_name = $fields[$article_name_index];
			$seq_no = $fields[$seq_no_index];
			if (empty($article_name)||empty($seq_no)){
				if ($k<(count($lines)-1)){
					echo "error: line $k , article_name seq_no 为空</br>"; 
				}else{
					echo "最后一行 line $k , article_name seq_no 为空</br>"; 
				}
				continue;
			}
			if (($k==0) && ($article_name=="稿件名称")){
				continue;
			}
			
			$chinese_key = $fields[$chinese_key_index];
			if (empty($chinese_key)){
				echo "Error: line $k chinese_key is empty</br>";
				continue;
			}
			if (!in_array($chinese_key,$chinese_key_list)){
				continue;
			}
			
			if ($fields[$englishTermIndex]=="句对错误")
				continue;
			
			if ($format==1){
				if (empty($fields[$sentenceEnIdx])){
					echo "English sentence line $k empty skipped </br>";
					continue;
				}
			}
			$temp_r = term::select("select id from term where chinese_key=\"$chinese_key\"");
			if (empty($temp_r)){
				echo "Error: line $k $chinese_key does not exist</br>";
				continue;
			}
			if (count($temp_r)>1){
				echo "Error: line $k $chinese_key has count ".count($temp_r)." in term db</br>";
				continue;
			}
			$term_id=$temp_r[0]->id;
			
			//0 - 普通， 1 - 特例（即不在术语的所有译法里），2 - 代词， 3 - 重复故省略(可以添加注释。少数情况，因为上一句有相同的词，所以省略了，需要说明是上一句)，4-隐含在其他词里， 98 - 未译， 99 - TBD 
			
			switch ($fields[$englishTermIndex]){
				case "代词":
					if (!empty($fields[$termCommentIndex])){
						$terms []= $fields[$termCommentIndex];
						$term_types[] = 2;
					} else{
						echo "error: line $k $article_name $seq_no 代词没找到</br>"; 
					}
					break;
					
				case "重复故省略":
					$terms[] = "";
					$term_types[] = 3;
					break;

				case "隐含在其他词里":
				case "隐含在其它词里":
					$terms[] = "";
					$term_types[] = 4;
					break;
					
				case "未译":
					$terms[] = "";
					$term_types[] = 98;
					break;
					
				case "TBD":
				case "没找到":
					$terms[] = "";
					$term_types[] = 99;
					break;
					
				case "语境错误":
					$terms[] = "";
					$term_types[] = 200;
					break;
					
				case "大词更合适":
					$terms[] = "";
					$term_types[] = 201;
					break;
					
				default:
					$terms[] = $fields[$englishTermIndex];
					$term_types[] = 1;
					//echo "term is ".$terms[count($terms)-1]."</br>";
					break;
			}
			if ($fields[$englishTermIndex]<>"代词")
				$term_comments[] = $fields[$termCommentIndex];
			else
				$term_comments[] = "";
			
			//if($k==90){
			//	echo "line is $line"." fields term is ".$terms[count($terms)-1]."</br>";
			//}
			
			$thisGroupEnd=false;
			if ($k==count($lines)-1){
				$thisGroupEnd=true;
			} else {
				if ($fileFormat = "txt"){
					$fields_next_line = explode($delimeter,$lines[$k+1]);
				}else{
					$fields_next_line = $cellData[$k+1];
				}
				if (empty($fields_next_line))
					$thisGroupEnd = true;
				else{ 
					if ( $article_name!==trim($fields_next_line[$article_name_index]) || ($seq_no!==trim($fields_next_line[$seq_no_index])) )
						$thisGroupEnd = true;
				}
			}
			if($thisGroupEnd==true){
				$this::importTermDetailCore($article_name, $seq_no, $term_id, $terms, $term_types, $term_comments,$option,$dryRun);
				$terms = $term_types = $term_comments = array();
			}
			
		}
		
	}*/

    public function importTermDetail()
    {
		set_time_limit(0);
		$dryRun = false;
		$option = "广论-0406-术语句对待提炼-HJB-List（28-100）合并by李舒";
		//$option="广论-0406-术语句对待提炼-27智慧";
		//$option="广论-0406-术语句对待提炼-1-27(21-27待校对）";
		//$option="5000-";
		//$option="final_1-5000";
		//$option="术语提炼-b3-0405-sherry-part3";
		//$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\文集4-佛法-lishu-v3.csv");
		//$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\因缘&境界-杨雪赵悦-20180306-王一晨-李舒.csv");
		//$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\术语提炼-第4-6册（汇总）.csv");
		switch($option){
			case "0404_1-5000":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\1-5000\\术语提炼-第4-6册（汇总）1-5000条审核 by wyn.csv");
				$chinese_key_list = array("修行经验","修行人","修行","修道","修学");
				$format=0;
				break;
			
			case "b3-0403-sherry":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\b3-0403\\术语提炼-b3-0403-output-sherry.txt");
				$chinese_key_list = array("修行者","学佛");
				$format=1;
				break;
				
			case "b3-0403-part2.2":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\b3-0403\\术语提炼-b3-0403-part2.2-correct-output.txt");
				$chinese_key_list = array("学修");
				$format=1;
				break;
				
			case "术语提炼-b3-0405-sherry-part3":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\b3-0403\\术语提炼-b3-0405-sherry-part3-output.txt");
				$chinese_key_list = array("有情","无情","修行者");
				$format=1;
				break;
				
			case "mtg0419":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\output\\句子序号\\术语提炼-第4-6册-句子序号（汇总）.xlsx");
				$chinese_key_list = array("希求","善巧","善巧方便","我执","法执","愿力","持戒");
				$format=2;
				$sheetIndex=1;
				break;
				
			case "final_1-5000":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\Output\\1-5000\\术语提炼-第4-6册（汇总）1-5000条审核 by wyn-v2(one sheet).xls");
				$chinese_key_list = array();
				$format = 1;
				break;
				
			case "5000-":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\Output\\句子序号\\术语提炼-第4-6册-句子序号（汇总）-v2(one sheet).xlsx");
				$chinese_key_list = array();
				$format = 2;
				break;
				
			case "广论-0406-术语句对待提炼-1-27(21-27待校对）":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\广论\\$option.xlsx");
				$chinese_key_list = array("菩萨","菩提","烦恼","如是","法门","在家","比丘","大乘","清净","持戒","功德","解脱","信心","大师","方便","忏悔","相应","善知识","妄语","次第","正法","精进","善法","成佛","发心","涅槃");
				$format = 2;
				break;
				
			case "广论-0406-术语句对待提炼-27智慧":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\广论\\文集7-8册高频100\\0423\\$option.xlsx");
				$chinese_key_list = array("智慧");
				$format = 2;
				break;
				
			case "广论-0406-术语句对待提炼-HJB-List（28-100）合并by李舒":
				$fileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\广论\\文集7-8册高频100\\0511\\$option.xlsx");
				$chinese_key_list = array("生死","声闻","弟子","无常","律师","增上","菩提道","瑜伽师地论","对治","无量","圆满","世俗","大智","知见","道次","菩提心","恭敬","身心","恶业","无上","菩提道次第广论","受戒","学戒","下品","思惟","利他","资粮","慈悲","正见","杀生","观察","出世","行者","究竟","安乐","善根","业障","受持","住持","法师","法师","福德","缘起","小乘","法相","法相","行善","依止","邪见","布施","自利","悲心","同行","道场","懈怠","正知","戒法","犯戒","般若","护持","出离","凡夫","不善","善友","誓愿","愿力","业道","佛菩萨","庄严","调伏","善业","大德","差别");
				$format = 2;
				break;				
		}
		
		switch($format){
			case 0:
				$fieldCount = 5;
				$delimeter = "\r\n";
				$fieldOffset = 0;
				$article_name_index = 0;
				$seq_no_index = 1;
				$chinese_key_index = 2;
				$englishTermIndex = 3;
				$termCommentIndex = 4;
				break;
				
			case 1:
				$fieldCount = 8;
				$delimeter = "\t";
				$fieldOffset = 2;
				$article_name_index = 1;
				$seq_no_index = 2;
				$sentenceCnIdx = 3;
				$sentenceEnIdx = 4;
				$chinese_key_index = 5;
				$englishTermIndex = 6;
				$termCommentIndex = 7;
				break;
				
			case 2:
				$fieldCount = 9;
				$delimeter = "\t";
				$article_name_index = 2;
				$seq_no_index = 3;
				$sentenceCnIdx = 4;
				$sentenceEnIdx = 5;
				$chinese_key_index = 6;
				$englishTermIndex = 7;
				$termCommentIndex = 8;
				break;
		}
		
		if(preg_match("/\.txt$/", $fileName)){
			$fileFormat = "txt";
		}else{
			$fileFormat = "excel";
		}
		
		if ($fileFormat=="txt"){
			$file = file_get_contents("$fileName");
			$lines = explode("\r\n",$file);
			$lineCount = count($lines);
		}else{
			//$cellData=ExcelActionController::myImportExcel($fileName);
			$cellData = [];  
			echo $fileName."</br>";
			Excel::load($fileName, function($reader) use( &$cellData ) {  
				$reader = $reader->getSheet(0);  
				$cellData = $reader->toArray();  
			});  			
			//var_dump($cellData);
			$lineCount = count($cellData);
		}
		echo $lineCount;
		//var_dump($cellData);
		
		$terms = $term_types = $term_comments = array();
		$lastSentenceOccurCnt = 0;
		//foreach($lines as $k=>$line){
		for($k=0;$k<$lineCount;$k++){
			if ($fileFormat=="txt"){
				$fields = explode($delimeter,$lines[$k]);
			}else{
				$fields = $cellData[$k];
			}
			
			for($i=0;$i<$fieldCount;$i++)
				$fields[$i] = empty($fields[$i]) ? "" : trim($fields[$i]);
			
			//var_dump($fields);continue;
			if (empty($fields)){
				if ($k<($$lineCount-1)){
					echo "error: line $k  为空</br>"; 
				}else{
					echo "最后一行 line $k  为空</br>"; 
				}
				continue;
			}
			$article_name = $fields[$article_name_index];
			$seq_no = $fields[$seq_no_index];
			if (empty($article_name)||empty($seq_no)){
				if ($k<($lineCount-1)){
					echo "error: line $k , article_name seq_no 为空</br>"; 
				}else{
					echo "最后一行 line $k , article_name seq_no 为空</br>"; 
				}
				continue;
			}
			if (($k==0) && ($article_name=="稿件名称")){
				continue;
			}
			
			$chinese_key = $fields[$chinese_key_index];
			if (empty($chinese_key)){
				echo "Error: line $k chinese_key is empty</br>";
				continue;
			}
			
			if (!empty($chinese_key_list)){
				if (!in_array($chinese_key,$chinese_key_list)){
					continue;
				}
			}
			
			if ($fields[$englishTermIndex]=="句对错误")
				continue;
			
			if ($format==1){
				if (empty($fields[$sentenceEnIdx])){
					echo "English sentence line $k empty skipped </br>";
					continue;
				}
			}
			$temp_r = term::select("select id from term where chinese_key=\"$chinese_key\"");
			if (empty($temp_r)){
				echo "Error: line $k $chinese_key does not exist</br>";
				continue;
			}
			if (count($temp_r)>1){
				echo "Error: line $k $chinese_key has count ".count($temp_r)." in term db</br>";
				continue;
			}
			$term_id=$temp_r[0]->id;
			
			//0 - 普通， 1 - 特例（即不在术语的所有译法里），2 - 代词， 3 - 重复故省略(可以添加注释。少数情况，因为上一句有相同的词，所以省略了，需要说明是上一句)，4-隐含在其他词里， 98 - 未译， 99 - TBD 
			
			switch ($fields[$englishTermIndex]){
				case "代词":
					if (!empty($fields[$termCommentIndex])){
						$terms []= $fields[$termCommentIndex];
						$term_types[] = 2;
					} else{
						echo "error: line $k $article_name $seq_no 代词没找到</br>"; 
					}
					break;
					
				case "重复故省略":
					$terms[] = "";
					$term_types[] = 3;
					break;

				case "隐含在其他词里":
				case "隐含在其它词里":
					$terms[] = "";
					$term_types[] = 4;
					break;
					
				case "未译":
					$terms[] = "";
					$term_types[] = 98;
					break;
					
				case "TBD":
				case "没找到":
					$terms[] = "";
					$term_types[] = 99;
					break;
					
				case "语境错误":
					$terms[] = "";
					$term_types[] = 200;
					break;
					
				case "大词更合适":
					$terms[] = "";
					$term_types[] = 201;
					break;
					
				default:
					$terms[] = $fields[$englishTermIndex];
					$term_types[] = 1;
					//echo "term is ".$terms[count($terms)-1]."</br>";
					break;
			}
			if ($fields[$englishTermIndex]<>"代词")
				$term_comments[] = $fields[$termCommentIndex];
			else
				$term_comments[] = "";
			
			/*if($k==90){
				echo "line is $line"." fields"term is ".$terms[count($terms)-1]."</br>";
			}*/
			
			$thisGroupEnd=false;
			if ($k==$lineCount-1){
				$thisGroupEnd=true;
			} else {
				if ($fileFormat == "txt"){
					$fields_next_line = explode($delimeter,$lines[$k+1]);
				}else{
					$fields_next_line = $cellData[$k+1];
				}
				if (empty($fields_next_line))
					$thisGroupEnd = true;
				else{ 
					if ( $article_name!==trim($fields_next_line[$article_name_index]) || ($seq_no!==trim($fields_next_line[$seq_no_index])) )
						$thisGroupEnd = true;
				}
			}
			if($thisGroupEnd==true){
				$this::importTermDetailCore($article_name, $seq_no, $term_id, $terms, $term_types, $term_comments,$option,$dryRun);
				$terms = $term_types = $term_comments = array();
			}
			
		}
		
	}
	
    public function importTermDetailCore($article_name, $seq_no, $term_id, $terms, $term_types, $term_comments,$option,$dryRun)
    {
		/*if ( ($term_id==17662) && ($seq_no==432)){
			print_r($terms);print_r($term_types);print_r($term_comments);
		}*/
		//just for debugging
		$tempChineseKey = term::select("select chinese_key from term where id=$term_id");
		if (!empty($tempChineseKey))
			$chineseKeyInThisFunction = $tempChineseKey[0]->chinese_key;
		else{
			echo "Error, $term_id not found in term table</br>";
			return;
		}
			
		if (in_array($option,array("0404_1-5000"))){
			$occur_no_field = "occur_no_batch_0";
			$sql_has_big_term = "1";
		}
		else{
			$occur_no_field = "occur_no";
			$sql_has_big_term = "has_big_term=0";
		}
		
		$article_name = str_replace("-0","_0",$article_name);
		$r = (article::select("select id from article where name=\"$article_name\""));
		if (empty($r))	{
			echo "error article_id for name=$article_name not found</br>";
			return false;
		}
		$article_id = $r[0]->id;
		echo "$article_id $seq_no ".count($terms)."</br>";
		
		$r = (sentence::select("select id,translation from sentence where article_id=$article_id and seq_no=$seq_no"));
		$sentence_id = $r[0]->id;
		$curSentence = $r[0]->translation;
		
		$term_details = (term_detail::select("select id term_detail_id from term_detail where sentence_id=$sentence_id and term_id=$term_id order by $occur_no_field and $sql_has_big_term"));
		if (empty($r)){
			echo "error: $article_name $seq_no 术语为空</br>"; 
		}
		$term_detail_count = count($term_details);
		
		/* do not check count match so commented out
		if (count($terms)!=$term_detail_count){
			
			$tooBad=true;
			if ($term_detail_count==0){
				$temp_r = term::select("select chinese_key from term where id=$term_id");
				$expectedTerm = $temp_r[0]->chinese_key;
				$temp_r = term_detail::select("select sentence.content,chinese_key from term_detail,sentence, term where term_detail.sentence_id=sentence.id and term_detail.article_id=$article_id and sentence.seq_no=$seq_no  and term_detail.term_id=term.id and $sql_has_big_term");
				$containingExpectedTerm = "";
				//echo $expectedTerm;		var_dump($temp_r);
				foreach($temp_r as $temp_v){
					if (mb_strpos($temp_v->chinese_key,$expectedTerm)!==false){
						$containingExpectedTerm = $temp_v->chinese_key;
						$tooBad=false;
						echo "not too bad, $containingExpectedTerm is found</br>";
					}
				}
				//echo "containingExpectedTerm = $containingExpectedTerm </br>";
				if (!empty($containingExpectedTerm))
					echo "{$temp_v->content}</br>";
			}
			if ($tooBad){
				echo "Error: $article_name $seq_no count not match. The counts from file and db are ".count($terms)." and $term_detail_count respectivley</br>";
			}
			return;
		}
		*/
		
		$fromWordInSentence = 0;
		//echo "occur_no={$termDetail->occur_no}</br>";
		foreach($term_details as $occur_no_i=>$term_detail){
			echo "occur_no_i=$occur_no_i</br>";
			//a very specail case, type=3 - 包含在其他词里
			if ($occur_no_i>=count($term_types)){
				echo "Warning: $chineseKeyInThisFunction $occur_no_i greater than # of inputs from file, ignored</br>";
				continue;
			}
			
			if ($term_types[$occur_no_i]>=3){
				if ($dryRun){
					echo " type={$term_types[$occur_no_i]}, term_comment=\"".addslashes($term_comments[$occur_no_i])."</br>";
				}else{
					term_detail::select("update term_detail SET english=\"\",english_pos=\"\", type={$term_types[$occur_no_i]}, term_comment=\"".addslashes($term_comments[$occur_no_i])."\" where id={$term_detail->term_detail_id}") ;
					echo "updated term_type>=3</br>";
				}
				continue;
			} 
			
			//temp
			$terms[$occur_no_i] = str_replace("-"," ",$terms[$occur_no_i]);
			if (empty($terms[$occur_no_i])){
				echo "warning term $chineseKeyInThisFunction $occur_no_i is empty, ignored</br>";
				continue;
			}
			
			$r = pos_in_sentence($terms[$occur_no_i], $curSentence,$fromWordInSentence);
			if(!empty($r)){
				////echo "***found</br>";
				//echo $curSentence."</br>";
				$start_len_arr = array();
				$lastStartWordPos = 0;
				foreach($r as $k=>$v){
					$start_len_arr []= $v['start'].",".$v['len'];
					$lastStartWordPos = $v['startWordPos'];
				}
				//var_dump($r);
				$start_len = implode(";",$start_len_arr);
				$pass_pos_in_sentence=100;
				if (!$dryRun){
					$sql = "update term_detail SET `english`=\"$terms[$occur_no_i]\", `english_pos`=\"$start_len\", type={$term_types[$occur_no_i]}, term_comment=\"".addslashes($term_comments[$occur_no_i])."\" , pass_pos_in_sentence=$pass_pos_in_sentence where id={$term_detail->term_detail_id}";	
					term_detail::select($sql);
					//echo "term is {$terms[$occur_no_i]} </br>";
					echo "$chineseKeyInThisFunction updated</br>";
				}
				////foreach($r as $k=>$v)	echo mb_substr($curSentence,$v['start'],$v['len'])." ";
				////echo "</br>";
			} else{
				echo "*** Error not found but updated</br>";
				echo "sentence is $curSentence</br>";
				echo "term is ".$terms[$occur_no_i]."</br>";
				$pass_pos_in_sentence = 1;
				if (!$dryRun){
					$sql = "update term_detail SET `english`=\"$terms[$occur_no_i]\", type={$term_types[$occur_no_i]}, term_comment=\"".addslashes($term_comments[$occur_no_i])."\" ,pass_pos_in_sentence=$pass_pos_in_sentence where id={$term_detail->term_detail_id}";	
				}
				continue;
			}
			
			$fromWordInSentence = $lastStartWordPos + 1;
		}
	}

	public function recoverTermDetail(){
		//to recover missing where in "update term_detail SET english=\"\",english_pos=\"\", type={$term_types[$occur_no_i]}, term_comment=\"".addslashes($term_comments[$occur_no_i])."\""
		set_time_limit(0);
		$r = term_detail::select("select * from term_detail");
		foreach($r as $term_detail){
			$id = $term_detail->id;
			echo "$id";
			$r2 = term_detail::select("select * from term_detail_old where id=$id");
			
			//affected fields: english, english_pos, type, term_comment
			if (!empty($r2)){
				echo " found in old";
				$term_detail_old = $r2[0];
				term_detail::select("update term_detail SET english=\"{$term_detail_old->english}\",english_pos=\"{$term_detail_old->english_pos}\", type=\"\", term_comment=\"\" where id=$id");
			} else{
				echo " NOT found in old";
				term_detail::select("update term_detail SET english=\"\",english_pos=\"\", type=\"\",term_comment=\"\"  where id=$id");
			}
			echo "</br>";
			ob_flush();flush();
		}
	}

	public function exportTermDetailByTerm(){
		$option = "lamrim";
		switch($option){
			case "wj":
				$outputFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\Output\\b3-0403\\b3-0405-part3.txt");			
				$termDetailBatchSql="term_detail.batch=30";
				$termChineseKeySLengthSql="1";
				$orderSql = "order by term_id,substring(article.name,3,1),sentence.seq_no,term_detail.occur_no";
				break;
				
			case "lamrim":
				$outputFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\广论\\广论-0406.txt");
				$termChineseKeySLengthSql="length(chinese_key)>=6";
				$termDetailBatchSql="term_detail.batch=900";				
				$orderSql = "order by term.`freq_in_4-8` desc,sentence.seq_no,term_detail.occur_no";
				break;
		}
		$termChineseKeys = "";//'"修行者","有情","无情"';
		if (!empty($termChineseKeys)){
			$termChineseKeySql="chinese_key in $termChineseKeys";
		}else{
			$termChineseKeySql="1";
		}
		
//术语提炼-b3-0403-part2-correct.txt");
		file_put_contents($outputFileName,"");
		$sentences = sentence::select("select term.chinese_key, article.name, sentence.seq_no, sentence.content, sentence.translation,term.chinese_key from sentence, term_detail, term, article where sentence.id=term_detail.sentence_id and term_detail.term_id=term.id and term_detail.article_id=article.id and $termChineseKeySql and $termChineseKeySLengthSql and  $termDetailBatchSql and term_detail.has_big_term=0 $orderSql");	
		//var_dump($sentences);exit;
		
		$outputSeqNo = 1;
		foreach($sentences as $sentence){
			file_put_contents( $outputFileName, $outputSeqNo."|".$sentence->name."|".$sentence->seq_no."|".$sentence->content."|".$sentence->translation."|".$sentence->chinese_key."|".""."|".""."\r\n",8);
			$outputSeqNo++;
		}
	}
	
	public function exportTermDetail(){
		$tempTableName = "temp456";
		$input="file";
		
		$inputFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\术语提炼-第4~6册-raw.txt");
		$outputSeqNoStart = 5001;
		$outputFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\术语提炼-第4~6册-句子序号.txt");
		
		term::select("delete from $tempTableName");
		if ($input=="db"){
		//$sql = 'select term.id, count(term.id) count, term.chinese_key,phrase, category from term_detail, term where term_detail.article_id between 4 and 28 and (term.source like "%DFB%" or term.source like "%wj%") and term_detail.term_id=term.id and type<100 group by term.id';
		$sql = 'select term.id, count(term.id) count, term.chinese_key,phrase, category, chinese_pos from term_detail, term where  (term_detail.article_id between 4 and 28) and (term.source like "%DFB%" or term.source like "%wj%") and (term_detail.term_id=term.id) and (type<100)  group by term.id';
		
		$r = term_detail::select($sql);
		//print_r($r);
		$term_id_list = array();
		foreach($r as $v){
			if ($v->count >= 5 || in_array($v->category,array("bookname","buudhaname","patriarchname"))){
				//echo "{$v->chinese_key}</br>";
				$term_id_list []= $v->id;
			}
		}
		//var_dump($term_id_list);exit;
		
		$term_ids=implode(",",$term_id_list);
		$sql = "select term_detail.id id, term_detail.article_id, term_detail.sentence_id, term_detail.english, term_detail.type, term_detail.term_comment, term.chinese_key, sentence.content,sentence.translation,sentence.seq_no, article.name from article,term_detail, sentence,term where term_detail.article_id between 4 and 28 and term.id in ($term_ids) and term_detail.sentence_id=sentence.id and term_detail.article_id=article.id and term_detail.term_id=term.id  and ((chinese_key not in (\"发心\",\"因缘\",\"境界\")) or (chinese_pos=0)) and ((chinese_key<>\"佛法\") or (term_detail.article_id not between 11 and 17) or (chinese_pos=0)) order by category,chinese_key,article.name, sentence.seq_no";//and (term_detail.term_id not in (17662,17149,1) or chinese_pos=0) and (term_detail.term_id<>22741 or term_detail.article_id not between 11 and 17 or chinese_pos=0)
		$r = term_detail::select($sql);
		$outputSeqNo=1;
		foreach($r as $term_detail){
			/*if ($term_detail->type==1 || $term_detail->type==2){
				//$start_len_arr = explode(";",$term_detail->english_pos)
			}*/
			//echo $term_detail->id."|".$term_detail->name."|".$term_detail->seq_no."|".$term_detail->type."</br>";
			if (empty($term_detail->type)){
				$term=$term_comment="";	
			}else{
			switch ($term_detail->type){
				case 1:
					$term=$term_detail->english;
					$term_comment = $term_detail->term_comment;
					break;
					
				case 2:	
					$term = "代词";
					$term_comment = $term_detail->english;
					break;
					
				case 3:
					$term = "重复故省略";
					$term_comment = $term_detail->term_comment;
					break;

				case 4:
					$term = "隐含在其他词里";
					$term_comment = $term_detail->term_comment;
					break;

				case 98:
					$term = "未译";
					$term_comment = $term_detail->term_comment;
					break;

				case 99:
					$term = "TBD";
					$term_comment = $term_detail->term_comment;
					break;
					
				default:
					echo "Error: term type not found</br>";
					break;
			}
			}
			//echo 				$term_detail->name."|".$term_detail->seq_no."|".$term_detail->content."|".$term_detail->translation."|".$term."|".$term_comment."</br>";
			
			$term_detail->content = str_replace("\n"," ",str_replace("\r"," ",$term_detail->content));
			$term_detail->translation = str_replace("\n"," ",str_replace("\r"," ",$term_detail->translation));
			if ($outputSeqNo<$outputSeqNoStart){
//				file_put_contents( iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\术语提炼-第4~6册.txt"), $term_detail->name."|".$term_detail->seq_no."|".$term_detail->content."|".$term_detail->translation."|".$term_detail->chinese_key."|".$term."|".$term_comment."\r\n",8);
				file_put_contents( iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\术语提炼-第4~6册.txt"), $outputSeqNo."|".$outputSeqNo."|".$term_detail->name."|".$term_detail->seq_no."|".$term_detail->content."|".$term_detail->translation."|".$term_detail->chinese_key."|".$term."|".$term_comment."\r\n",8);
			}
			term::select("insert into $tempTableName (output_seq_no, article_id, article_name,sentence_id,seq_no,sentence_cn, sentence_en, term_cn, term_en, term_comment) values (".
				"{$outputSeqNo},".
				"{$term_detail->article_id},".
				"\"{$term_detail->name}\",".
				"{$term_detail->sentence_id},".
				"{$term_detail->seq_no},".
				"\"".addslashes($term_detail->content)."\",".
				"\"".addslashes($term_detail->translation)."\",".
				"\"".addslashes($term_detail->chinese_key)."\",".
				"\"".addslashes($term)."\",".
				"\"".addslashes($term_detail->term_comment)."\"".
				")");
			$outputSeqNo ++;
		}
		}
		else{
			$file=file_get_contents($inputFileName);
			$lines=explode("\r\n",$file);
			$outputSeqNo=1;
			foreach($lines as $line){
				if (empty($line))
					continue;
				$fields=explode("|",$line);
				$article_name = $fields[0];
				$seq_no = $fields[1];
				$sentence_cn = $fields[2];
				$sentence_en = $fields[3];
				$term_cn = $fields[4];
				$term_en = $fields[5];
				$term_comment = $fields[6];
				$temp_r = article::select("select id from article where name=\"$article_name\"");
				if (empty($temp_r)){
					echo "Error\<br>";
					continue;
				}
				$article_id = $temp_r[0]->id;
				$temp_r = sentence::select("select id from sentence where article_id=$article_id and seq_no=$seq_no");
				if (empty($temp_r)){
					echo "Error\<br>";
					continue;
				}
				$sentence_id = $temp_r[0]->id;
				
				if (false){
					file_put_contents( $outputFileName, $outputSeqNo."|".$outputSeqNo."|".$fields[0]."|".$fields[1]."|".$fields[2]."|".$fields[3]."|".$term_detail->$fields[4]."|".$fields[5]."|".$fields[6]."\r\n",8);
				}
				if ($outputSeqNo>=$outputSeqNoStart){
					term::select("insert into $tempTableName (output_seq_no, article_id, article_name,sentence_id,seq_no,sentence_cn, sentence_en, term_cn, term_en, term_comment) values (".
					"{$outputSeqNo},".
					"{$article_id},".
					"\"{$article_name}\",".
					"{$sentence_id},".
					"{$seq_no},".
					"\"".addslashes($sentence_cn)."\",".
					"\"".addslashes($sentence_en)."\",".
					"\"".addslashes($term_cn)."\",".
					"\"".addslashes($term_en)."\",".
					"\"".addslashes($term_comment)."\"".
					")");
				}
				$outputSeqNo ++;
			}
		}
		
		//generate output file in new format;
		$outputSeqNo = $outputSeqNoStart-1;
		$sentences=sentence::select("
		select sentence.id, seq_no, sentence.content, sentence.translation, article.name from sentence, article where sentence.article_id=article.id and article_id between 4 and 28 order by article.name, seq_no
		");
		foreach($sentences as $sentence){
			$temp_r=sentence::select("select * from $tempTableName where sentence_id={$sentence->id} order by output_seq_no");
			if (empty($temp_r)){
				$sentence->content = str_replace("\n"," ",str_replace("\r"," ",$sentence->content));
				$sentence->translation = str_replace("\n"," ",str_replace("\r"," ",$sentence->translation));

				file_put_contents( $outputFileName, $outputSeqNo."|"."无"."|".$sentence->name."|".$sentence->seq_no."|".$sentence->content."|".$sentence->translation."|"."无"."|".""."|".""."\r\n",8);
				
			} 
			else{
				foreach($temp_r as $temp_v){
					if($outputSeqNoStart <=$temp_v->output_seq_no){
						file_put_contents( $outputFileName, $outputSeqNo."|".$temp_v->output_seq_no."|".$temp_v->article_name."|".$temp_v->seq_no."|".$temp_v->sentence_cn."|".$temp_v->sentence_en."|".$temp_v->term_cn."|".$temp_v->term_en."|".$temp_v->term_comment."\r\n",8);
						$outputSeqNo++;
					}
				}
			}
		}
	}
	
	public function extractBookNameFromSentence(){
		set_time_limit(0);
		$dryRun = true;
		$r = sentence::select("select content from sentence where content like \"%《%\" OR \"%》%\" and article_id between 4 and 28");
		foreach($r as $v){
			$content = $v->content;
			//file_put_contents( iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\经论名句子-第4~6册.txt"),"{$v->content}\r",8);
			$startPos=$endPos=0;
			$foundCount=0;
			while($endPos<mb_strlen($content)){
				$startPos = mb_strpos($content,"《",$endPos);
				if ($startPos === false)
					break;
				$endPos = mb_strpos($content,"》",$startPos);
				if ($endPos === false){
					echo "Error, start without end</br>";
					break;
				}
				$foundCount++;
				$foundSubstring = trim(mb_substr($content,$startPos+1,$endPos-$startPos-1));
				//echo "Found ".$foundSubstring." in $content</br>";
				$r2 = term::select("select id,source,category from term where chinese_key=\"$foundSubstring\"");
				if (empty($r2)){
					echo "$foundSubstring not found in term </br>";
					if ($dryRun)
						echo "insert INTO term (chinese_key, source, category) VALUES (\"$foundSubstring\",\"wj\",\"bookname\")";
					else
						term::select( "insert INTO term (chinese_key, source, category) VALUES (\"$foundSubstring\",\"wj\",\"bookname\")");
				}else{
					$source = $r2[0]->source;
					$id = $r2[0]->id;
					if (strpos($source,"wj")===false){
						echo "$foundSubstring found but not marked, source is $source </br>";
						$source = (empty($source)) ? "wj" : "$source,wj";
						if ($dryRun)
							echo "update term SET source=\"$source\", category=\"bookname\" where id=$id</br>";
						else
							term::select( "update term SET source=\"$source\", category=\"bookname\" where id=$id");
					} else{
						if ($r2[0]->category<>"bookname"){
							if ($dryRun)
								echo "update term SET category=\"bookname\" where id=$id";
							else
								term::select( "update term SET category=\"bookname\" where id=$id");
						}else{
							echo "OK</br>";
						}
						
					}
				}
				//file_put_contents( iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\经论名句子-第4~6册.txt"),"$foundSubstring\r",8);
			}
			if ($foundCount==0)
				echo "Error, not found in this $content</br>";
		}
	}
	
	public function fillTermDetailBookname(){
//先从sentence抽取带书名号
//同一个句子有没有这个术语（严格匹配），
//  如无，则加入
//处理contained，仅限于bookname
	$dryRun = true;
	$verifyInsert = true;
	set_time_limit(0);
	$sentences = sentence::select("select content,id sentence_id,article_id from sentence where content like \"%《%\" OR \"%》%\" and article_id between 4 and 28");
	foreach($sentences as $sentence){
		$content = $sentence->content;
		$sentence_id = $sentence->sentence_id;
		$article_id = $sentence->article_id;
		
		//extract book name from sentence
		$startPos=$endPos=0;
		$foundCount=0;
		while($endPos<mb_strlen($content)){
			$startPos = mb_strpos($content,"《",$endPos);
			if ($startPos === false)
				break;
			$endPos = mb_strpos($content,"》",$startPos);
			if ($endPos === false){
				echo "Error, start without end</br>";
				break;
			}
			$cur_occur_no = $foundCount;
			$foundCount++;
			$cur_chinese_pos = $startPos + 1;
			$bookname = $foundSubstring = trim(mb_substr($content,$startPos+1,$endPos-$startPos-1));
			
			//lookup extracted book name from term
			$temp_r = term::select("select * from term where chinese_key=\"$bookname\" and (source like \"%DFB%\" or source like \"%wj%\")");
			if (empty($temp_r)){
				echo "$bookname NOT FOUND in term DB</br>";
				break;
			}
			$bookname_term_id = $temp_r[0]->id;
			
			//echo "Found ".$foundSubstring." in $content</br>";
			$temp_r = term::select("select id term_detail_id from term_detail where term_id=$bookname_term_id and sentence_id=$sentence_id and occur_no=$cur_occur_no");
			
			if(empty($temp_r)){
				echo "$bookname and sentence_id $sentence_id NOT found in term_detail DB</br>";
				$sql = "insert INTO term_detail (chinese_pos, occur_no, sentence_id, article_id, term_id) VALUES (".
					           implode(",",array($cur_chinese_pos,$cur_occur_no,$sentence_id,$article_id,$bookname_term_id)).")";
				if ($dryRun)
					echo "$sql <br>";
				else{
					term_detail::insert($sql);
					if ($verifyInsert){
						$sql = sprintf("select id term_detail_id from term_detail where term_id=$bookname_term_id and sentence_id=$sentence_id and occur_no=%d",$foundCount-1);
						echo "sql=$sql</br>";
						$temp_r = term::select($sql);
						$termDetailIdToCheck = $temp_r[0]->term_detail_id;
						$verifyResult = $this::checkTermDetailById($termDetailIdToCheck);
						echo "verifyResult=".($verifyResult)?"OK":"BAD"."</br>";
					}
				}
			}else{
				//echo "$bookname and sentence_id $sentence_id FOUND in term_detail DB</br>";
				//better to check and then update
				//term_detail::select( "UPDATE term_detail SET term_id=$bookname_term_id , article_id=$article_id, sentence_id=$sentence_id , occur_no=$cur_occur_no, chinese_pos= $cur_chinese_pos where id={$temp_r[0]->term_detail_id}");	
				//echo "updated</br>";
			}
			
			//process the terms which are substring of the bookname
			$sql = "select term_detail.id term_detail_id, chinese_key,chinese_pos from term, term_detail where term.id=term_detail.term_id and sentence_id=$sentence_id and term.id<>$bookname_term_id and term.contained_in like \"$bookname_term_id\" and occur_no=$cur_occur_no";
			$sub_term_details = term_detail::select($sql);
			
			if (empty($sub_term_details)){
				continue;
			}
			
			echo "Process Contained-in Sentence is </br> $content </br> bookname is $bookname </br>";
			foreach($sub_term_details as $sub_term_detail){
				$sub_key_pos_in_big_key = mb_strpos($bookname,$sub_term_detail->chinese_key);
				if ($sub_key_pos_in_big_key===false){
					//echo "{$sub_term_detail->chinese_key} NOT substring of $bookname</br>";
					continue;
				}
				echo "startPos=$startPos  sub_key_pos_in_big_key=$sub_key_pos_in_big_key chinese_pos_in_sub_term_detail={$sub_term_detail->chinese_pos}</br>";
				if ($cur_chinese_pos + $sub_key_pos_in_big_key == $sub_term_detail->chinese_pos){
					echo "{$sub_term_detail->chinese_key} is Part of you</br>";
					term::select("delete from term_detail where id={$sub_term_detail->term_detail_id}");
				} else{
					echo "{$sub_term_detail->chinese_key} is NOT part of you</br>";
				}
				
				//$sub_key_len = mb_strlen($sub_term_detail->chinese_key);
				//$sub_key_extracted_from_sentence = mb_substr($sub_term_detail->content,$sub_term_detail->chinese_pos,$sub_key_len);
				
			}
			echo "============================================== </br>";
		}
		
		}
		if ($foundCount==0)
			echo "Error, not found in this $content</br>";
	}
	
	public function searchBuddhaname(){
		set_time_limit(0);
		$r = term::select("select chinese_key from term where chinese_key like \"%禅师\"");
		foreach($r as $v){
			$r2 = sentence::select("select content from sentence where content like \"%{$v->chinese_key}%\" and article_id between 4 and 28");
			if(!empty($r2)){
				echo "term={$v->chinese_key}  count= ".count($r2)."</br>";
				ob_flush();flush();
			}
		}
	}
	
	public function fixArticleIdInTermDetail(){
		$term_details = term_detail::select("select term_detail.article_id article_id1, sentence.article_id article_id2, term_detail.id, chinese_key from term,term_detail, sentence where term.id=term_detail.id and term_detail.sentence_id=sentence.id");
		foreach($term_details as $term_detail){
			if ($term_detail->article_id1<>$term_detail->article_id2){
				echo "Error: {$term_detail->chinese_key} {$term_detail->id} article_id1={$term_detail->article_id1} article_id2={$term_detail->article_id2} </br>";
				term_detail::select("update term_detail SET article_id={$term_detail->article_id2} where id={$term_detail->id}");
			}
		}
	}
	
	//one time
	public function addMissing(){
		set_time_limit(0);
		$file = file_get_contents(iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\Missing.txt"));
		$lines = explode("\r\n",$file);
		foreach($lines as $line){
			$fields = explode("\t",$line);
			if (count($fields)<2)
				echo "error </br>";
			$chinese_key = $fields[0];
			$sentence_id = $fields[1];
			$temp_r = term::select("select * from term where chinese_key=\"$chinese_key\"");
			$term_id = $temp_r[0]->id;
			$temp_r = term::select("select * from sentence where id=$sentence_id");
			$article_id = $temp_r[0]->article_id;
			
			$temp_r = term::select("select * from term_detail where term_id=$term_id and sentence_id=$sentence_id");
			if (!empty($temp_r)){
				echo "Error</br>";
				continue;
			}
			$sql= "insert INTO term_detail (chinese_pos, occur_no, sentence_id, article_id, term_id) VALUES (". implode(",",array(0,0,$sentence_id,$article_id,$term_id)).")";
			term_detail::select($sql);
		}
	}
	
	public function delDupTermDetail(){
		set_time_limit(0);
		$dryRun=true;
		$temp_r=term_detail::select("select sentence_id, occur_no, term_id, count(*) count from term_detail group by sentence_id, occur_no, term_id");
		$dupCount = 0;
		foreach($temp_r as $v){
			if ($v->count==1)	continue;
			$temp_r2 = term_detail::select("select * from term_detail where sentence_id={$v->sentence_id} and occur_no={$v->occur_no} and term_id={$v->term_id}");
			$fields = array("chinese", "english", "article_id", "last_modifier", "last_modify_time", "chinese_pos", "english_pos", "tonggao_changed", "comment", "type", "term_comment");
			echo "will compare sentence_id={$v->sentence_id} and occur_no={$v->occur_no} and term_id={$v->term_id}</br> ";
			var_dump($temp_r2);
			foreach($fields as $field){
				$allSame=true;
				if ($temp_r2[0]->{$field}!==$temp_r2[1]->{$field}){
					echo "$field: {$temp_r2[0]->{$field}} vs {$temp_r2[1]->{$field}} </br>";
					$allSame=false;
				}
			}
			if (!$dryRun){
				term_detail::select("delete from term_detail where id={$temp_r2[1]->id}");
			}
			$dupCount++;
		}
		echo "dupCount=$dupCount</br>";
	}
	
    public function temp(){
		set_time_limit(0);
		$dryRun = true;
		
		$termDetails = array();
		for($i=0;$i<1;$i++)	$termDetails[$i]=new td_temp;
		$termDetails[0]->article_id  	= 9;
		$termDetails[0]->seq_no		  	= 126;
		$termDetails[0]->old_key	  	= "生死因缘";
		$termDetails[0]->new_key	  	= "因缘";
		
		/*$termDetails[0]->article_id  	= 10;
		$termDetails[0]->seq_no		  	= 482;
		$termDetails[0]->old_key	  	= "因缘生";
		$termDetails[0]->new_key	  	= "因缘";

		$termDetails[1]->article_id  	= 10;
		$termDetails[1]->seq_no		  	= 836;
		$termDetails[1]->old_key	  	= "";
		$termDetails[1]->new_key	  	= "因缘";

		$termDetails[2]->article_id  	= 12;
		$termDetails[2]->seq_no		  	= 546;
		$termDetails[2]->old_key	  	= "";
		$termDetails[2]->new_key	  	= "因缘";

		$termDetails[3]->article_id  	= 12;
		$termDetails[3]->seq_no		  	= 549;
		$termDetails[3]->old_key	  	= "";
		$termDetails[3]->new_key	  	= "因缘";

		$termDetails[0]->article_id  	= 10;
		$termDetails[0]->seq_no		  	= 482;
		$termDetails[0]->old_key	  	= "因缘生";
		$termDetails[0]->new_key	  	= "因缘";

		$termDetails[1]->article_id  	= 10;
		$termDetails[1]->seq_no		  	= 836;
		$termDetails[1]->old_key	  	= "";
		$termDetails[1]->new_key	  	= "因缘";

		$termDetails[2]->article_id  	= 12;
		$termDetails[2]->seq_no		  	= 546;
		$termDetails[2]->old_key	  	= "";
		$termDetails[2]->new_key	  	= "因缘";

		$termDetails[3]->article_id  	= 12;
		$termDetails[3]->seq_no		  	= 549;
		$termDetails[3]->old_key	  	= "";
		$termDetails[3]->new_key	  	= "因缘";

		$termDetails[0]->article_id  	= 10;
		$termDetails[0]->seq_no		  	= 482;
		$termDetails[0]->old_key	  	= "因缘生";
		$termDetails[0]->new_key	  	= "因缘";

		$termDetails[1]->article_id  	= 10;
		$termDetails[1]->seq_no		  	= 836;
		$termDetails[1]->old_key	  	= "观因缘";
		$termDetails[1]->new_key	  	= "因缘";

		$termDetails[2]->article_id  	= 12;
		$termDetails[2]->seq_no		  	= 546;
		$termDetails[2]->old_key	  	= "观因缘";
		$termDetails[2]->new_key	  	= "因缘";

		$termDetails[3]->article_id  	= 12;
		$termDetails[3]->seq_no		  	= 549;
		$termDetails[3]->old_key	  	= "观因缘";
		$termDetails[3]->new_key	  	= "因缘";

		$termDetails[4]->article_id  	= 13;
		$termDetails[4]->seq_no		  	= 11;
		$termDetails[4]->old_key	  	= "大事因缘";
		$termDetails[4]->new_key	  	= "因缘";

		$termDetails[5]->article_id  	= 14;
		$termDetails[5]->seq_no		  	= 182;
		$termDetails[5]->old_key	  	= "因缘生";
		$termDetails[5]->new_key	  	= "因缘";

		$termDetails[6]->article_id  	= 16;
		$termDetails[6]->seq_no		  	= 1157;
		$termDetails[6]->old_key	  	= "因缘生";
		$termDetails[6]->new_key	  	= "因缘";

		$termDetails[7]->article_id  	= 17;
		$termDetails[7]->seq_no		  	= 386;
		$termDetails[7]->old_key	  	= "因缘生";
		$termDetails[7]->new_key	  	= "因缘";

		$termDetails[8]->article_id  	= 21;
		$termDetails[8]->seq_no		  	= 226;
		$termDetails[8]->old_key	  	= "因缘生";
		$termDetails[8]->new_key	  	= "因缘";*/
		
		foreach($termDetails as $termDetail){
			if (!empty($termDetail->old_key)){
				$temp_r = term::select("select id from term where chinese_key=\"{$termDetail->old_key}\"");
				$oldTermId = $temp_r[0]->id;
			}
			$temp_r = term::select("select id from term where chinese_key=\"{$termDetail->new_key}\"");
			$newTermId = $temp_r[0]->id;
			
			$temp_r = sentence::select("select * from sentence where article_id={$termDetail->article_id} and seq_no={$termDetail->seq_no}");
			$content = $temp_r[0]->content;
			$sentence_id = $temp_r[0]->id;
			
			$chinese_pos = mb_strpos($content,$termDetail->new_key);
			
			if (empty($termDetail->old_key)){
				$sql = "INSERT INTO term_detail (article_id, sentence_id, occur_no, term_id, chinese_pos) VALUES ({$termDetail->article_id},$sentence_id,0,$newTermId,$chinese_pos)";
			}else{
				$temp_r = term_detail::select("select id from term_detail where sentence_id=$sentence_id and term_id=$oldTermId and occur_no=0");
				$id = $temp_r[0]->id;
				$sql = "UPDATE term_detail SET term_id=$newTermId , chinese_pos=$chinese_pos where id=$id";
			}
			if($dryRun){
				echo "$sql</br>";
			}else{
				term_detail::select($sql);
			}
		}
    }
	
	public function fillDemoId(){
		////刚加入"因缘"、"境界"和前5000条
		$terms_first5000 = array("因缘","境界","一切有情","作意","作礼","作佛","专行","住持","主宰","众生","丈夫","优婆塞","义利","义理","亿劫","仪轨","依止","依正","依报","一心念佛","一心不乱","一切有情","一切法","一念","业障","业缘","业有","业因","业力","业果","业感缘起","业感","修证","修学","修善","修行人","修行经验","修行","修得","修道","信仰","信心","信乐","信解","像法","下化众生","习气","五祖","五蕴","五欲","五明","五戒","五观","万行","万法","事相","世尊","世俗","世人","世间法","世间","僧众","僧团","僧俗","僧宝","三业","三学","三途","三世","三十七道品","三十二相","三善道","三千大千世界","三昧","三界","三归","三恶道","三毒","三藏","三宝","入心","人天","人身","人间","乞士","乞食","七宝","内缘","六因","六入","六根","六度","六道轮回","六道","六波罗蜜","临终","具足","净信","净土宗","净土","假名","供养","供奉","共业","共修","公案","佛子","佛语","佛学院","佛像","佛陀","佛堂","佛世","佛身","佛七","佛菩萨","佛门","佛经","佛教徒","佛教","佛果","佛国","佛法僧","佛法","佛道","分别","凡夫","二乘","二乘","丛林","串习","出世间","出世","出离心","出离","出家人","出家","忏悔业障","不退转","不退","不善","不可思议","不可得","不净观","不净","不变随缘","八正道","八万四千烦恼","八万四千法门","八风","作善","众生心","众会","众苦");
		
		$terms_mtg_20180329 = array	
		("戒律","律仪","波罗提木叉","戒律仪","根律仪","别解律仪","别解脱律仪","别解脱戒","尸罗律仪","威仪","律典","律藏","开遮","持戒","犯戒","制戒","结戒","戒本","戒条","性戒","遮戒","戒法","戒体","戒行","戒相","八关斋戒","沙弥戒","沙弥尼戒","比丘戒","比丘尼戒","菩萨戒","三聚戒","三聚净戒","摄律仪戒","摄善法戒","摄众生戒","饶益有情戒") ;
		
		$terms = $terms_mtg_20180329;
		foreach($terms as $k=>$term){
			$k1=$k+1;
			$temp_r = term::select("select id from term where chinese_key=\"$term\"");
			term::select("UPDATE term SET demo_id=".$k1." where id={$temp_r[0]->id}");
		}
	}
	
	public function importGL(){
		set_time_limit(0);
		$inputFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\广论\\语料库-菩提道次第广论-The Great Treatise on the Stages of the Path to Enlightenment-Lamrim_Export20180402214426.xlsx");
		Excel::load($inputFileName, function($reader) {
			$sheetIdx = 0;
			$batch=90;
			$sentenceTableName = "sentence";
			$temp_r = term::select("select id from article where name=\"gl\"");
			$article_id = $temp_r[0]->id;
			
			$reader = $reader->getSheet($sheetIdx);
			$results = $reader->toArray();
			
			foreach($results as $row_i=>$row){
				$sql = "insert INTO $sentenceTableName (article_id, seq_no, content, translation, batch) VALUES (".
					implode(",",array($article_id,$row_i+1,
						'"'.addslashes($row[0]).'"',
						'"'.addslashes($row[1]).'"',
						$batch)).
					")";
				sentence::select($sql);
			}
		});
	}
	
	public function searchTerm(){
		$debug1=true;
		$debug2=true;
		set_time_limit(0);
		
		$dirName = iconv('UTF-8','GB2312',"F:\\000_booktran\\佛教论坛\\0420");
		$fileNames = scandir($dirName);
		foreach($fileNames as $inputFileName){
			if(!preg_match("/\.txt$/", $inputFileName))
				continue;
			echo  iconv('GB2312','UTF-8',$inputFileName)."</br>";
			$outputFileName = iconv('UTF-8','GB2312',"F:\\000_booktran\\佛教论坛\\0420\\").preg_replace("/\.txt$/","_terms.txt",$inputFileName);
			file_put_contents($outputFileName,"");
			
			$input=file_get_contents(iconv('UTF-8','GB2312',"F:\\000_booktran\\佛教论坛\\0420\\").$inputFileName);
			$inputLen=mb_strlen($input);
			if ($debug2)	{echo mb_substr($input,0,min(300,$inputLen))."</br>";ob_flush();flush();}
			$terms = term::select("select chinese_key,chinese_key_len from term where chinese_key_len>3");
			if ($debug2)	{echo "select from term db completed</br>";	ob_flush();flush();}
			foreach($terms as $term){
				$chineseKey = $term->chinese_key;
				$chineseKeyLen = $term->chinese_key_len;
				if ($debug1)	{echo "search $chineseKey</br>"; ob_flush();flush();}
			
				$curTermCount=0;
				$startPos = 0;
				while($startPos<$inputLen) {
					$curFoundPos = mb_strpos($input,$chineseKey,$startPos);
					if ($curFoundPos===false)
						break;
					
					$bigNeedles = term::select("select chinese_key from term where chinese_key_len>$chineseKeyLen and chinese_key like \"%chineseKey%\"");

					$foundInBig = false;
					foreach($bigNeedles as $bigNeedle){
						$bigChineseKey = $bigNeedle->chinese_key;
						$smallInBigPos = mb_strpos($bigChineseKey,$chineseKey);
						$bigTermPosInSentence = $curFoundPos - $smallInBigPos;
						if ($bigTermPosInSentence<0)
							continue;
						$bigPieceInSentence = mb_substr($input,$bigTermPosInSentence,mb_strlen($bigChineseKey));
						if ($bigPieceInSentence==$bigChineseKey){
							if ($debug1)	{echo "Big found! $chineseKey contained in $bigChineseKey</br>"; ob_flush();flush();}
							$foundInBig=true;
						}
					}
					
					if ($foundInBig==false)
						$curTermCount++;
					
					$startPos = $curFoundPos+1; 
				}
				if ($curTermCount>0)
					{file_put_contents($outputFileName,"$chineseKey	$curTermCount\r\n",8); ob_flush();flush();}
			}
		}
	}
	
	public function tMyImportExcel(){
		//$inputFileName=iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\Output\\1-5000\\术语提炼-第4-6册（汇总）1-5000条审核 by wyn - simple check.xlsx");
		 $inputFileName=iconv('UTF-8','GB2312',"F:\\000_booktran\\数据准备\\Output\\1-5000\\术语提炼-第4-6册（汇总）1-5000条审核 by wyn - simple check - test one sheet.xlsx");
		ExcelActionController::myImportExcel($inputFileName);
	}
} //!- PrepareController
?>

